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

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

2 声明数组变量:

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

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 }

























原文地址:https://www.cnblogs.com/jasonHome/p/5734054.html