定义
//初始化一个长度为8的定长数组,其所有元素均为0
val arr1 = new Array[Int](_8)
//直接打印定长数组,内容为数组的hashcode值
println(arr1)
//将数组转换为数组缓冲,就可以看到原数组的内容了
//toBuffer会将数组转换为长数组缓冲
println(arr1.toBuffer)
//注意: 如果new,相当于调用了数组的apply方法,直接为数组赋值
//初始化一个长度为1的定长数组
val arr2 = Array[Int](_10)
println(arr1.toBuffer)
//定义一个长度为3的定长数组
val arr3 = Array("hadoop", "storm", "spark")
//使用()来访问元素
println(arr3(2))
赋值及取值
//等价于 arr2(1)=10
scala> arr2.update(1,10)
scala> arr2
res6: Array[Int] = Array(1, 10, 3)
也可以通过apply来取值
scala> arr2.apply(1)
res9: Int = 10
变长数组
需要打包
# _表示导入这个包下的所有
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val ab = new ArrayBuffer[Int]()
ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> val arrBuffer1 = scala.collection.mutable.ArrayBuffer[Int](_10_9_8)
arrBuffer1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 9, 8)
追加元素
# 向尾部追加
scala> ab += 1
res0: ab.type = ArrayBuffer(1)
scala> ab += (3, 4, 5)
res1: ab.type = ArrayBuffer(1, 3, 4, 5)
# 追加一个数组缓冲
scala> ab ++= Array(6, 7)
res2: ab.type = ArrayBuffer(1, 3, 4, 5, 6, 7)
# 追加一个数组缓冲
scala> ab ++= ArrayBuffer(8, 9)
res3: ab.type = ArrayBuffer(1, 3, 4, 5, 6, 7, 8, 9)
# 向尾部追加
ab.append(123)
指定位置插入用insert
# 在0这个位置插入-1和0
ab.insert(0, -1, 0)
会生成一个新的数组
scala> ab :+ 99
res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(123, 3, 99)
scala> ab
res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(123, 3)
删除
scala> ab
res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 3, 4, 5, 6, 7, 8, 9)
# 删除数组某个位置的元素
# 在3这个位置移除2个
scala> ab.remove(3, 2)
scala> ab
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 5, 6, 7, 8, 9)
定长数组和变长数组的转换
scala> arr2.toBuffer
res7: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 10, 3)
scala> arr2.toArray
res8: Array[Int] = Array(1, 10, 3)
排序
scala> val arr = Array(1, 9, 5, 7, 3, 2)
arr: Array[Int] = Array(1, 9, 5, 7, 3, 2)
升序
scala>arr.sorted
res0: Array[Int] = Array(1, 2, 3, 5, 7, 9)
降序
scala> arr.sorted.reverse
res2: Array[Int] = Array(9, 7, 5, 3, 2, 1)
scala> arr.sortWith(_>_)
res1: Array[Int] = Array(9, 7, 5, 3, 2, 1)
也可以这样写
scala> arr.sortWith( (x, y) => x > y )
res3: Array[Int] = Array(9, 7, 5, 3, 2, 1)
转换后排序
scala> val arr = Array(1,2,3,4,10,100)
arr: Array[Int] = Array(1, 2, 3, 4, 10, 100)
scala> arr.sortBy(x => x+"")
res0: Array[Int] = Array(1, 10, 100, 2, 3, 4)
数组转换
yield关键字将原始的数组进行转换会产生一个新的数组,原始的数组不变
scala> var arr = Array(1, 2, 3, 4, 5, 6, 7)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
scala> val res = for(e <- arr) yield e*2
res: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14)
scala> arr.map(_ * 2)
res0: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14)
scala> arr
res1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
过滤
偶数乘以10
scala> arr.filter(_ % 2 == 0).map(_ * 10)
res2: Array[Int] = Array(20, 40, 60)
常用方法
求和
scala> arr
res3: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
scala> arr.sum
res4: Int = 28
//reduce汇总
scala> arr.reduce(_+_)
res5: Int = 28
给初始值求和
scala> arr.fold(10)(_+_)
res1: Int = 38
求最大值
arr.max
切分和压平
flatten和map()
scala> val a = Array("a b c", "d e f", "h i j")
a: Array[String] = Array(a b c, d e f, h i j)
scala> a.map(_.split(" "))
res1: Array[Array[String]] = Array(Array(a, b, c), Array(d, e, f), Array(h, i, j))
scala> a.flatMap(_.split(" "))
res4: Array[String] = Array(a, b, c, d, e, f, h, i, j)
转换为String
scala> val arr2 = Array(1,2,3)
arr2: Array[Int] = Array(1, 2, 3)
scala> arr2.mkString(",")
res4: String = 1,2,3
多维数组
定义
scala> val arr4 = Array.ofDim[Int](_3_ 4)
arr4: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))
scala> arr4.mkString(" ")
res16: String = [I@22b8eaf0 [I@50dfceff [I@26a5007
scala> for(x <- arr4) println(x.mkString(","))
0,0,0,0
0,0,0,0
0,0,0,0
赋值
scala> arr4(1)(2) = 1
与java数组互相转换
scala数组转java数组
import scala.collection.mutable.ArrayBuffer
val arr4 = ArrayBuffer("1", "2", "3")
// scala to java
import scala.collection.JavaConversions.bufferAsJavaList
val javaArr = new ProcessBuilder(arr4)
println(javaArr.command())
java数组转scala数组
import scala.collection.mutable.ArrayBuffer
val arr4 = ArrayBuffer("1", "2", "3")
//_表示导入这个包下的所有
import scala.collection.JavaConversions._
import scala.collection.mutable.Buffer
// scala to java
val javaArr = new ProcessBuilder(arr4)
//java to scala
val scalaArr: Buffer[String] = javaArr.command()
println(scalaArr)