定长数组定义:
val ar = new Array[Int](10)
val arr = Array("aa", "bb")
定长数组赋值:
arr(0) = "cc"
变长数组定义:
val ab = new ArrayBuffer[String]()
val ab1 = ArrayBuffer[String]()
定长数组增加元素:
ab += "aa"
ab += ("bb", "cc")
ab ++= Array("dd")
ab ++= List("ee", "ff")
定长数组转变长数组:
(ab.toArray[String]).toBuffer
变长数组转定长数组:
ab.toArray
数组循环:
for(i <- 0 until (arr.length, 1)){
println(arr(i))
}
for(el <- arr){
println(el)
}
for(i <- (0 until (arr.length, 1)).reverse){
println(arr(i))
}
数组转换(不修改原始数组,而产生一个全新数组):
val rs = for(el <- arr if el != null) yield {el + "sky"}
rs.foreach(println)
数组常用操作:
val y = ArrayBuffer(5,4,3,1,2)
println(y.sortWith(_ < _))
println(Array(1,2).sum)
println(ArrayBuffer(1,2).sum)
val yy = Array(5,4,3,1,2)
println(yy.sortWith(_ < _).toBuffer) scala.util.Sorting.quickSort(yy)
println(yy.toBuffer)
println(yy.mkString("=", ",", "="))