Scala学习笔记(四)—— 数组
定长数组Array
定义定长数组用Array,有如下几种方法:
- 初始化一个长度为8的定长数组,其所有元素默认值均为0
1 scala> new Array[Int](8) 2 res0: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)
- 调用静态方法,直接为数组赋值
1 scala> val arr2 = Array[Int](8) 2 arr2: Array[Int] = Array(8) 3 4 scala> arr2.length 5 res16: Int = 1 6 7 scala> Array(1,"1") 8 res17: Array[Any] = Array(1, 1) 9 10 scala> val arr3 = Array(1,"string",3.14) 11 arr3: Array[Any] = Array(1, string, 3.14) 12 13 scala> val arr4 = Array(1,2,3) 14 arr4: Array[Int] = Array(1, 2, 3)
变长数组ArrayBuffer
- 需要导入import scala.collection.mutable.ArrayBuffer包
1 scala> import scala.collection.mutable.ArrayBuffer 2 import scala.collection.mutable.ArrayBuffer
- 初始化有以下两种方法:
1 scala> import scala.collection.mutable.ArrayBuffer 2 import scala.collection.mutable.ArrayBuffer 3 4 scala> val ab =new ArrayBuffer[Int]() 5 ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() 6 7 scala> val ab1 = ArrayBuffer(1,2) 8 ab1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2)
- 使用+=追加(多个)元素,使用++= 追加数组
1 scala> ab += 1 2 res0: ab.type = ArrayBuffer(1) 3 4 scala> ab += 2 5 res1: ab.type = ArrayBuffer(1, 2) 6 7 scala> ab += (3,4,5) 8 res2: ab.type = ArrayBuffer(1, 2, 3, 4, 5) 9 10 scala> ab ++= Array(6,7) 11 res4: ab.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7) 12 13 scala> ab ++= ArrayBuffer(8,9) 14 res5: ab.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
- 指定位置插入元素:第一个入参是指定要插入的下标,后面的元素则插入在该下标及其之后
1 scala> ab 2 res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6,7, 8, 9) 3 4 scala> ab.insert(0,0) 5 6 scala> ab 7 res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 8 9 scala> ab.length 10 res3: Int = 10 11 12 scala> ab.insert(11,11) 13 java.lang.IndexOutOfBoundsException: 11 14 at scala.collection.mutable.ArrayBuffer.insertAll(ArrayBuffer.scala:139) 15 at scala.collection.mutable.BufferLike$class.insert(BufferLike.scala:167) 16 at scala.collection.mutable.AbstractBuffer.insert(Buffer.scala:49) 17 ... 32 elided 18 19 scala> ab.insert(10,10) 20 21 scala> ab 22 res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
注意:插入的下标 <= 该数组的长度,若插入的下标为数组的长度则表示在末尾追加;若插入的下标大于该数组的长度,则报错数组越界 IndexOutOfBoundsException
- 删除指定位置的元素:第一个入参是指定要删除的下标;第二个入参是要删除的数据的长度,默认值为1
1 scala> ab.length 2 res9: Int = 11 3 4 scala> ab.remove(11) 5 java.lang.IndexOutOfBoundsException: 11 6 at scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:43 7 ) 8 at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:48) 9 at scala.collection.mutable.ArrayBuffer.remove(ArrayBuffer.scala:169) 10 ... 32 elided 11 12 scala> ab.remove(10) 13 res11: Int = 10 14 15 scala> ab 16 res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 17 18 scala> ab.remove(9,2) 19 java.lang.IndexOutOfBoundsException: 9 20 at scala.collection.mutable.ArrayBuffer.remove(ArrayBuffer.scala:158) 21 ... 32 elided 22 23 scala> ab.length 24 res14: Int = 10 25 26 scala> ab.remove(9,1) 27 28 scala> ab 29 res16: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8)
注意:要删除的下标 < 数组的长度, 否则报错数组越界 IndexOutOfBoundsException
- 遍历数组
请参考笔记(二)for循环
- 数组常用算法
用filter方法进行过滤
scala> ab res22: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 6, 7, 8) scala> ab.filter(_ % 2 == 0) res23: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 2, 4, 6, 8)
用map方法进行数组转换
1 scala> ab 2 res24: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8) 3 4 scala> ab.map(_ * 10) 5 res25: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 10, 20, 30, 40, 50, 60, 70, 80)
例:将数组中偶数取出来乘以100
1 scala> ab 2 res26: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8) 3 4 scala> ab.filter(_ % 2 == 0).map(_*100) 5 res27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 200, 400, 600, 800)
数组求和、最小值、最大值、排序
scala> ab res35: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 10, 50, 30, 5, 6, 7, 8) scala> ab.sum res36: Int = 126 scala> ab.min res38: Int = 0 scala> ab.max res39: Int = 50 scala> ab.sorted res40: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 30, 50)