ArrayBuffer 相当于ArrayList 是有序的。
val a = ArrayBuffer[Int]() a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> a+= 1 res4: a.type = ArrayBuffer(1) scala> a+= 2 res5: a.type = ArrayBuffer(1, 2) scala> a+= 4 res6: a.type = ArrayBuffer(1, 2, 4)
a+=(3,4,5,7)
res7: a.type = ArrayBuffer(1, 2, 4, 3, 4, 5, 7)
a.trimEnd(n)移除a 元素里面最后的n个值
res7: a.type = ArrayBuffer(1, 2, 4, 3, 4, 5, 7) scala> a.trimEnd(3) scala> a res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 4, 3)
a.remove(n,m) : 移除第n个下标起的,m个元素
scala> a res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 4, 3, 10, 2, 3, 4, 5) scala> a.remove(2,2) scala> a res13: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 10, 2, 3, 4, 5)
遍历数组
res14: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8) scala> for (i <- 0 until (arr.length,2)){ | println(arr(i)) | } 1 3 5 7
映射
scala> val m = Map("a" ->1, "b"->2, "c"->5) m: scala.collection.mutable.Map[String,Int] = Map(b -> 2, a -> 1, c -> 5) scala> for ((k,v) <- m) yield (v,k) res18: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 5 -> c, 1 -> a)
元组