定义

  1. //初始化一个长度为8的定长数组,其所有元素均为0
  2. val arr1 = new Array[Int](_8)
  3. //直接打印定长数组,内容为数组的hashcode值
  4. println(arr1)
  5. //将数组转换为数组缓冲,就可以看到原数组的内容了
  6. //toBuffer会将数组转换为长数组缓冲
  7. println(arr1.toBuffer)
  8. //注意: 如果new,相当于调用了数组的apply方法,直接为数组赋值
  9. //初始化一个长度为1的定长数组
  10. val arr2 = Array[Int](_10)
  11. println(arr1.toBuffer)
  12. //定义一个长度为3的定长数组
  13. val arr3 = Array("hadoop", "storm", "spark")
  14. //使用()来访问元素
  15. println(arr3(2))

赋值及取值

  1. //等价于 arr2(1)=10
  2. scala> arr2.update(1,10)
  3. scala> arr2
  4. res6: Array[Int] = Array(1, 10, 3)

也可以通过apply来取值

  1. scala> arr2.apply(1)
  2. res9: Int = 10

变长数组

需要打包

  1. # _表示导入这个包下的所有
  2. scala> import scala.collection.mutable._
  3. import scala.collection.mutable._
  4. scala> val ab = new ArrayBuffer[Int]()
  5. ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
  6. scala> val arrBuffer1 = scala.collection.mutable.ArrayBuffer[Int](_10_9_8)
  7. arrBuffer1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 9, 8)

追加元素

  1. # 向尾部追加
  2. scala> ab += 1
  3. res0: ab.type = ArrayBuffer(1)
  4. scala> ab += (3, 4, 5)
  5. res1: ab.type = ArrayBuffer(1, 3, 4, 5)
  6. # 追加一个数组缓冲
  7. scala> ab ++= Array(6, 7)
  8. res2: ab.type = ArrayBuffer(1, 3, 4, 5, 6, 7)
  9. # 追加一个数组缓冲
  10. scala> ab ++= ArrayBuffer(8, 9)
  11. res3: ab.type = ArrayBuffer(1, 3, 4, 5, 6, 7, 8, 9)
  12. # 向尾部追加
  13. ab.append(123)

指定位置插入用insert

  1. # 在0这个位置插入-1和0
  2. ab.insert(0, -1, 0)

会生成一个新的数组

  1. scala> ab :+ 99
  2. res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(123, 3, 99)
  3. scala> ab
  4. res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(123, 3)

删除

  1. scala> ab
  2. res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 3, 4, 5, 6, 7, 8, 9)
  3. # 删除数组某个位置的元素
  4. # 在3这个位置移除2个
  5. scala> ab.remove(3, 2)
  6. scala> ab
  7. res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 5, 6, 7, 8, 9)

定长数组和变长数组的转换

  1. scala> arr2.toBuffer
  2. res7: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 10, 3)
  3. scala> arr2.toArray
  4. res8: Array[Int] = Array(1, 10, 3)

排序

  1. scala> val arr = Array(1, 9, 5, 7, 3, 2)
  2. arr: Array[Int] = Array(1, 9, 5, 7, 3, 2)

升序

  1. scala>arr.sorted
  2. res0: Array[Int] = Array(1, 2, 3, 5, 7, 9)

降序

  1. scala> arr.sorted.reverse
  2. res2: Array[Int] = Array(9, 7, 5, 3, 2, 1)
  1. scala> arr.sortWith(_>_)
  2. res1: Array[Int] = Array(9, 7, 5, 3, 2, 1)

也可以这样写

  1. scala> arr.sortWith( (x, y) => x > y )
  2. res3: Array[Int] = Array(9, 7, 5, 3, 2, 1)

转换后排序

  1. scala> val arr = Array(1,2,3,4,10,100)
  2. arr: Array[Int] = Array(1, 2, 3, 4, 10, 100)
  3. scala> arr.sortBy(x => x+"")
  4. res0: Array[Int] = Array(1, 10, 100, 2, 3, 4)

数组转换

yield关键字将原始的数组进行转换会产生一个新的数组,原始的数组不变

  1. scala> var arr = Array(1, 2, 3, 4, 5, 6, 7)
  2. arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
  3. scala> val res = for(e <- arr) yield e*2
  4. res: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14)
  5. scala> arr.map(_ * 2)
  6. res0: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14)
  7. scala> arr
  8. res1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)

过滤

偶数乘以10

  1. scala> arr.filter(_ % 2 == 0).map(_ * 10)
  2. res2: Array[Int] = Array(20, 40, 60)

常用方法

求和

  1. scala> arr
  2. res3: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
  3. scala> arr.sum
  4. res4: Int = 28
  5. //reduce汇总
  6. scala> arr.reduce(_+_)
  7. res5: Int = 28

给初始值求和

  1. scala> arr.fold(10)(_+_)
  2. res1: Int = 38

求最大值

  1. arr.max

切分和压平
flatten和map()

  1. scala> val a = Array("a b c", "d e f", "h i j")
  2. a: Array[String] = Array(a b c, d e f, h i j)
  3. scala> a.map(_.split(" "))
  4. res1: Array[Array[String]] = Array(Array(a, b, c), Array(d, e, f), Array(h, i, j))
  5. scala> a.flatMap(_.split(" "))
  6. res4: Array[String] = Array(a, b, c, d, e, f, h, i, j)

转换为String

  1. scala> val arr2 = Array(1,2,3)
  2. arr2: Array[Int] = Array(1, 2, 3)
  3. scala> arr2.mkString(",")
  4. res4: String = 1,2,3

多维数组

定义

  1. scala> val arr4 = Array.ofDim[Int](_3_ 4)
  2. arr4: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))
  3. scala> arr4.mkString(" ")
  4. res16: String = [I@22b8eaf0 [I@50dfceff [I@26a5007
  5. scala> for(x <- arr4) println(x.mkString(","))
  6. 0,0,0,0
  7. 0,0,0,0
  8. 0,0,0,0

赋值

  1. scala> arr4(1)(2) = 1

与java数组互相转换

scala数组转java数组

  1. import scala.collection.mutable.ArrayBuffer
  2. val arr4 = ArrayBuffer("1", "2", "3")
  3. // scala to java
  4. import scala.collection.JavaConversions.bufferAsJavaList
  5. val javaArr = new ProcessBuilder(arr4)
  6. println(javaArr.command())

java数组转scala数组

  1. import scala.collection.mutable.ArrayBuffer
  2. val arr4 = ArrayBuffer("1", "2", "3")
  3. //_表示导入这个包下的所有
  4. import scala.collection.JavaConversions._
  5. import scala.collection.mutable.Buffer
  6. // scala to java
  7. val javaArr = new ProcessBuilder(arr4)
  8. //java to scala
  9. val scalaArr: Buffer[String] = javaArr.command()
  10. println(scalaArr)