变长数组的创建
先调包再创建
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> var arr1 = ArrayBuffer("a","b")
arr1: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(a, b)
arr1 +="a"//添加内容
arr1 +=("a","ac")
//可以用append添加
arr1.append(2)
scala> arr1
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 3, 1, 2)
//删除第一个元素
arr1 -= 1
res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(3, 3, 1, 2)
\\ arr1.trimEnd(1)从尾部删除一个
\\arr1.trimStart(1)从开始删除一个
数组变换
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> for(e <-a ) yield e*10
res20: Array[Int] = Array(10, 20, 30, 40, 50)
scala> a.map(x=>x*10)
res24: Array[Int] = Array(10, 20, 30, 40, 50)
scala> a.map(_*10)
res21: Array[Int] = Array(10, 20, 30, 40, 50)
scala> a.filter(_ %2 == 0)
res23: Array[Int] = Array(2, 4)