Array
与Java的Array类似,也是长度不可变的数组,此外,由于Scala与Java都是运行在JVM中,双方可以互相调用,因此Scala数组的底层实际上是Java数组。
注意:访问数组中元素使用()而不是Java中的 []
scala> val a = new Array[String](10)a: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)scala> val a = new Array[Boolean](10)a: Array[Boolean] = Array(false, false, false, false, false, false, false, false, false, false)scala> a(0)res15: Boolean = falsescala> val a = new Array[Int](10)a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)scala> a(0) = 1
可以直接使用Array()创建数组,元素类型自动推断(如果类型不一致则为公共父类型)
scala> val a = Array("Hello", "World")a: Array[String] = Array(Hello, World)scala> val a = Array("Sparks", 30)a: Array[Any] = Array(Sparks, 30)// 常见操作scala> val a = Array(3,4,1,2,5,3)a: Array[Int] = Array(3, 4, 1, 2, 5, 3)scala> val sum = a.sumsum: Int = 18scala> val max = a.maxmax: Int = 5scala> scala.util.Sorting.quickSort(a)scala> ares35: Array[Int] = Array(1, 2, 3, 3, 4, 5)scala> a.mkStringres36: String = 123345scala> a.mkString(",")res37: String = 1,2,3,3,4,5scala> a.mkString("<",",",">")res38: String = <1,2,3,3,4,5>// Array的toString有些问题scala> a.toStringres39: String = [I@ffd26d1scala> b.toStringres40: String = ArrayBuffer(1, 6, 7, 8, 9, 10)
ArrayBuffer
类似于Java中的ArrayList长度可变集合类
scala> import scala.collection.mutable.ArrayBufferimport scala.collection.mutable.ArrayBufferscala> val b = ArrayBuffer[Int]()b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()// 使用+=添加一个或者多个元素,spark源码中大量使用!!!scala> b += 1res17: b.type = ArrayBuffer(1)scala> b += (2,3,4,5)res18: b.type = ArrayBuffer(1, 2, 3, 4, 5)// 使用++=添加其他集合中的所有元素scala> b ++= Array(6, 7, 8, 9, 10)res19: b.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)// trimEnd方法可以从尾部截断指定个数的元素scala> b.trimEnd(5)scala> bres21: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)// 使用insert()函数可以在指定位置插入一个或者多个元素,效率较低scala> b.insert(5,6)scala> b.insert(6, 7, 8, 9, 10)scala> bres24: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)// 使用remove()函数可以移除指定位置的一个或者多个元素scala> b.remove(1)res25: Int = 2scala> b.remove(1, 3)scala> bres27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 6, 7, 8, 9, 10)// Array与ArrayBuffer互相转化scala> b.toArrayres28: Array[Int] = Array(1, 6, 7, 8, 9, 10)scala> a.toBufferres29: scala.collection.mutable.Buffer[Any] = ArrayBuffer(Sparks, 30)
遍历Array和ArrayBuffer
使用until是RichInt提供的函数
for (i <- 0 until b.length) print(b(i))跳跃遍历,步进长度
for (i <- 0 until (b.length, 2)) print(b(i))从尾部遍历
for (i <- (0 until b.length).reverse) println(b(i))使用“增强for循环”遍历
for (e <- b) println(e)
数组操作之数组转换
1.使用yield
scala> val a = Array(1,2,3,4,5)a: Array[Int] = Array(1, 2, 3, 4, 5)scala> val a2 = for(ele <- a) yield ele*elea2: Array[Int] = Array(1, 4, 9, 16, 25)scala> val a = ArrayBuffer(1,2,3,4,5)a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)scala> val a2 = for(ele <- a) yield ele*elea2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 4, 9, 16, 25)
2.使用函数式编程
_ 表示通配符- a.filter(_ % 2 == 0).map(2 * _) (推荐方式)
- a.filter{ _ % 2 == 0} map {2 * _}
实战:移除第一个负数之后的所有负数
// 构建数组scala> val a = ArrayBuffer[Int]()a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()scala> a += (1,2,3,4,5,-1,-3,-5,-9)res45: a.type = ArrayBuffer(1, 2, 3, 4, 5, -1, -3, -5, -9)// 发现第一个负数之后的负数,就进行移除,性能较差,多次移动数组var foundFirstNegative = falsevar index = 0while(index < a.length) { if (a(index) >= 0) { index += 1 } else { if (!foundFirstNegative) {foundFirstNegative=true; index += 1} else { a.remove(index)} }}//检索出所有需要元素的下标,然后将所有需要的元素左移至数组左侧,删除右侧不需要的元素。//改良版,性能较高var foundFirstNegative = falseval vaildIndex = for (i <- 0 until a.length if (!foundFirstNegative || a(i) >= 0)) yield { if (a(i) < 0) foundFirstNegative = true i}for(i <- 0 until vaildIndex.length) {a(i) = a(vaildIndex(i))}a.trimEnd(a.length - vaildIndex.length)