[原创]Scala学习:数组的基本操做,数组进阶操做,多维数组

 

 

1.Scala中提供了一种数据结构-数组,其中存储相同类型的元素的固定大小的连续集合。数组用于存储数据的集合,但它每每是更加有用认为数组做为相同类型的变量的集合javascript

 

2 声明数组变量:java

要使用的程序的数组,必须声明一个变量来引用数组,必须指定数组变量能够引用的类型。下面是语法声明数组变量:es6

var z:Array[String] = new Array[String](3)     or      var z = new Array[String](3) or var z = Array("Zara", "Nuha", "Ayan")



在这里,z被声明为字符串数组,最多可容纳三个元素。能够将值分配给独立的元素或能够访问单个元素,这是能够作到经过使用相似于如下命令:数组

 
z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan"


3.Scala中数组方法:
 

如下是重要的方法,能够同时使用数组。如上所示,则必须使用任何说起的方法以前,要导入Array._包。有关可用方法的完整列表,请Scala中的官方文件。数据结构

 

 

SN 方法及描述
1 def apply( x: T, xs: T* ): Array[T]
建立T对象,其中T能够是Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean数组。
2 def concat[T]( xss: Array[T]* ): Array[T]
链接全部阵列成一个数组。
3 def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit
复制一个数组到另外一个。至关于Java的System.arraycopy(src, srcPos, dest, destPos, length).
4 def empty[T]: Array[T]
返回长度为0的数组
5 def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T]
返回一个包含一个函数的重复应用到初始值的数组。
6 def fill[T]( n: Int )(elem: => T): Array[T]
返回包含某些元素的计算的结果的次数的数组。
7 def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]
返回一个二维数组,其中包含某些元素的计算的结果的次数。
8 def iterate[T]( start: T, len: Int)( f: (T) => T ): Array[T]
返回一个包含一个函数的重复应用到初始值的数组。
9 def ofDim[T]( n1: Int ): Array[T]
建立数组给出的尺寸。
10 def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]]
建立了一个2维数组
11 def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]]
建立3维数组
12 def range( start: Int, end: Int, step: Int ): Array[Int]
返回包含一些整数间隔等间隔值的数组。
13 def range( start: Int, end: Int ): Array[Int]
返回包含的范围内增长整数序列的数组。
14 def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]
返回包含一个给定的函数的值超过从0开始的范围内的整数值的数组。
15 def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]
返回一个包含给定函数的值超过整数值从0开始范围的二维数组。






  1 package first.scala  2 
 3 import scala.collection.mutable.ArrayBuffer  4 import sun.org.mozilla.javascript.internal.ast.Yield  5 
 6 object ScalaInAction {  7  //scala.Array
 8     
 9     /******************************************************************************************************************************/
 10     //定长数组  11     //声明数组方式一:类型,大小
 12     val nums = new Array[Int](10)                 //> nums : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
 13     val a = new Array[String](10)                 //> a : Array[String] = Array(null, null, null, null, null, null, null, null, n  14                                                   //| ull, null)  15     //声明方式二:能够经过类型推断,推断出数组的类型
 16     val s = Array("hello" , "world")              //> s : Array[String] = Array(hello, world)
 17     
 18     s(0) = "goodbye"
 19     
 20     
 21     //可变数组
 22    val b = ArrayBuffer[Int]()                     //> b : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
 23      
 24     b += 1                                        //> res0: first.scala.ScalaInAction.b.type = ArrayBuffer(1)
 25     b += (1,2,3,4)                                //> res1: first.scala.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4)
 26     b ++= Array(12,15,63)                         //> res2: first.scala.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4, 12, 15,  27                                                   //| 63)  28     //删除最后的2个元素
 29     b.trimEnd(2)  30     b                                             //> res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4,  31                                                   //| 12)  32     //给定索引处插入 数据
 33     b.insert(2, 15)  34     b                                             //> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 15, 2, 3  35                                                   //| , 4, 12)  36     
 37    //删除索引为2的元素
 38     b.remove(2)                                   //> res5: Int = 15  39     
 40     //转换为数组,类型的变换
 41     b.toArray                                     //> res6: Array[Int] = Array(1, 1, 2, 3, 4, 12)
 42     b                                             //> res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4,  43                                                   //| 12)
 44                
 45     /*******************************************************************************************************/
 46     //数组的进阶操做
 47     
 48     for(i <- 0 until a.length)  49         println(i + " : " + a(i))                 //> 0 : null  50                                                   //| 1 : null  51                                                   //| 2 : null  52                                                   //| 3 : null  53                                                   //| 4 : null  54                                                   //| 5 : null  55                                                   //| 6 : null  56                                                   //| 7 : null  57                                                   //| 8 : null  58                                                   //| 9 : null
 59     
 60     
 61     val c = Array(2,5,8,9,18)                     //> c : Array[Int] = Array(2, 5, 8, 9, 18)
 62          val  result = for(elem <- c) yield 2 * elem  63                                                   //> result : Array[Int] = Array(4, 10, 16, 18, 36)  64     
 65     //将c中的偶数乘2
 66     for(elem <- c if elem % 2 == 0 ) yield 2 * elem  67                                                   //> res8: Array[Int] = Array(4, 16, 36)  68           
 69     //spark中方式,和上面的效果同样。先过滤后map
 70     c.filter( _ % 2 == 0).map(2 * _)              //> res9: Array[Int] = Array(4, 16, 36)  71     
 72     //求和
 73     Array(1,2,3).sum                              //> res10: Int = 6  74     
 75     
 76     //获取最长的字符串
 77     ArrayBuffer("Mary", "had", "a", "little", "lamb").max  78                                                   //> res11: String = little  79     
 80     //排序,默认升序排序
 81     val d = ArrayBuffer(1,7,2,9)                  //> d : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
 82     val bSorted = d.sorted                        //> bSorted : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7,  83                                                   //| 9)  84                                                   
 85     //快速排序
 86     val e = Array(1,7,2,9)                        //> e : Array[Int] = Array(1, 7, 2, 9)
 87  scala.util.Sorting.quickSort(e)  88     
 89     //定义元素链接方式
 90     e.mkString(" and ")                           //> res12: String = 1 and 2 and 7 and 9  91     //定义元素链接方式
 92     a.mkString("<", "," , ">")                    //> res13: String = <null,null,null,null,null,null,null,null,null,null>
 93     
 94     
 95   /**************************************************************************************************************************/
 96     
 97     
 98       //定义多维数组方法: Array.ofDim[Double](3,4)
 99     val matrix = Array.ofDim[Double](3,4)         //> matrix : Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0 100                                                   //| , 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0))
101     matrix(2)(1) = 42
102     
103     matrix                                        //> res14: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0 104                                                   //| .0, 0.0, 0.0), Array(0.0, 42.0, 0.0, 0.0))
105         val triangle = new Array[Array[Int]](10) 106                                                   //> triangle : Array[Array[Int]] = Array(null, null, null, null, null, null, n 107                                                   //| ull, null, null, null)
108                                   
109                                   
110         for(i <- 0 until triangle.length) 111             triangle(i) = new Array[Int](i + 1) 112             triangle                  //> res15: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Arr 113                                                   //| ay(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 114                                                   //| 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0 115                                                   //| , 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
116     
117     
118                                             
119                                                  
120 }
相关文章
相关标签/搜索