1.創建和更新數組的不同方式
1)定義一個數組的初始大小和類型,隨後填充值。
scala> val array = new Array[String](3) array: Array[String] = Array(null, null, null) scala> array(0) = "abc" scala> array(1) = "def" scala> array(2) = "hij" scala> array res66: Array[String] = Array(abc, def, hij)
2)創建指向數組的var引用,然後賦值給它。
scala> var fruits: Array[String] = _ fruits: Array[String] = null scala> fruits = Array("apple","orange","banana") fruits: Array[String] = [Ljava.lang.String;@79a73e5e scala> fruits res67: Array[String] = Array(apple, orange, banana)
3)集合混合類型組成。
scala> val x =Array(1,2.0,33D,400L) x: Array[Double] = Array(1.0, 2.0, 33.0, 400.0)
4)集合混合類型組成,可以有自己控制。下面的例子的數組保持了原有數組的類型。
scala> val x =Array[Number](1,2.0,33D,400L) x: Array[Number] = Array(1, 2.0, 33.0, 400)
5)range創建和填充數組。
scala> val x = Array.range(1,10) x: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> val x = Array.range(1,10,2) x: Array[Int] = Array(1, 3, 5, 7, 9)
6)fill創建和填充數組。
scala> val x = Array.fill(3)("hello") x: Array[String] = Array(hello, hello, hello)
7)tabulate創建和填充數組。
scala> val x = Array.tabulate(5)(i => i * i) x: Array[Int] = Array(0, 1, 4, 9, 16)
8)將list集合轉化爲數組的形式。
scala> val x = List(1,2,3,4,5).toArray x: Array[Int] = Array(1, 2, 3, 4, 5)
9)將字符串轉化爲數組的形式。
scala> "hello".toArray res68: Array[Char] = Array(h, e, l, l, o)
10)通過()的索引,就可以訪問對應的元素。
scala> val x = Array(1,3,4,5,7,9) x: Array[Int] = Array(1, 3, 4, 5, 7, 9) scala> x(0) res70: Int = 1 scala> x(3) res71: Int = 5
11)更新數組就是把訪問的元素給到一個值即可完成更新。
scala> x(3) = 30 scala> x res73: Array[Int] = Array(1, 3, 4, 30, 7, 9)
2.創建大小可變的數組(ArrayBuffer)
1)Array是可變的,因爲它的內容可以改變,但是其大小是不能進行改變的。想要創建一個大小和值都可變的索引序列,可以使用ArrayBuffer。
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> var x = ArrayBuffer[String]() x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer() scala> x += "aa" res74: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa) scala> x += "bb" res75: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb) scala> x += "cc" res76: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb, cc) scala> x res77: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb, cc)
2)可以在創建ArrayBuffer時就添加元素,之後繼續添加。
scala> val x = collection.mutable.ArrayBuffer("aa","bb") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb) scala> x += "cc" res78: x.type = ArrayBuffer(aa, bb, cc)
3)可以使用++=添加元素到數組中去。
scala> val x = collection.mutable.ArrayBuffer("aa","bb") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb) scala> x ++= Seq("dd","ee") res79: x.type = ArrayBuffer(aa, bb, dd, ee)
4)可以使append方法添加元素到數組中去。
scala> val x = collection.mutable.ArrayBuffer("aa","bb") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb) scala> x.append("ww","oo","pp") scala> x res81: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(aa, bb, ww, oo, pp)
3.刪除Array和ArrayBuffer中的元素
1)使用-=刪除一個或者多個元素。
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val x = ArrayBuffer("c","d","a","e","f") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f) scala> x -= "a" res82: x.type = ArrayBuffer(c, d, e, f) scala> x -= ("c","d") res83: x.type = ArrayBuffer(e, f)
2)使用--=刪除定義在另一個集合中的多個元素。
scala> val x = ArrayBuffer("c","d","a","e","f") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f) scala> x --= Seq("a","c") res84: x.type = ArrayBuffer(d, e, f) scala> x --= Array("d") res85: x.type = ArrayBuffer(e, f) scala> x --= Set("f") res86: x.type = ArrayBuffer(e)
3)根據ArrayBuffer中元素的位置使用remove刪除元素,或者根據開始位置刪除一系列的元素。
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val x = ArrayBuffer("c","d","a","e","f") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f) scala> x.remove(0) res0: String = c scala> x res1: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(d, a, e, f) scala> x.remove(1,3) scala> x res3: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(d)
4)clear方法可以刪除數組中的所有元素。
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val x = ArrayBuffer("c","d","a","e","f") x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(c, d, a, e, f) scala> x.clear scala> x res5: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
5)定義數組變量var,通過過濾的方式刪除元素,重新將結果賦給自己。
scala> var a = Array("apple","orange","banana") a: Array[String] = Array(apple, orange, banana) scala> a = a.take(2) a: Array[String] = [Ljava.lang.String;@405223e4 scala> a res6: Array[String] = Array(apple, orange)
4.數組的排序
將一個數組或者ArrayBuffer中的元素進行排序。如果正在使用的是一個包含隱式排序元素的Array,可以使用scala.util.Sorting.quickSort方法對該Array進行排序。
scala> val x = Array("cherry","apple","banana") x: Array[String] = Array(cherry, apple, banana) scala> scala.util.Sorting.quickSort(x) scala> x res8: Array[String] = Array(apple, banana, cherry) scala> val x = Array(2,4,1,9,5,7) x: Array[Int] = Array(2, 4, 1, 9, 5, 7) scala> scala.util.Sorting.quickSort(x) scala> x res10: Array[Int] = Array(1, 2, 4, 5, 7, 9)
5.創建多維數組
1)使用Array.ofDim方法創建所需要的數組。
scala> val rows = 2 rows: Int = 2 scala> val cols = 3 cols: Int = 3 scala> val x = Array.ofDim[String](rows,cols)
2)給多維數組添加元素。
scala> x(0)(0) = "a" scala> x(0)(1) = "b" scala> x(0)(2) = "c" scala> x(1)(0) = "d" scala> x(1)(1) = "e" scala> x(1)(2) = "f" scala> x res17: Array[Array[String]] = Array(Array(a, b, c), Array(d, e, f))
3)根據索引的具體位置訪問元素。
scala> val a = x(1)(1) a: String = e
用for循環遍歷數組的元素:
scala> for { | i <- 0 until rows | j <- 0 until cols | } println(s"($i)($j) = ${x(i)(j)}") (0)(0) = a (0)(1) = b (0)(2) = c (1)(0) = d (1)(1) = e (1)(2) = f
4)創建三維數組。
scala> val x,y,z = 3 x: Int = 3 y: Int = 3 z: Int = 3 scala> val a = Array.ofDim[Int](x,y,z) a: Array[Array[Array[Int]]] = Array(Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0,0, 0), Array(0, 0, 0), Array(0, 0, 0))) scala> for { | i <- 0 until x | j <- 0 until y | z <- 0 until z | } println(s"($i)($j)($z) = ${a(i)(j)(z)}") (0)(0)(0) = 0 (0)(0)(1) = 0 (0)(0)(2) = 0 (0)(1)(0) = 0 (0)(1)(1) = 0 (0)(1)(2) = 0 (0)(2)(0) = 0 (0)(2)(1) = 0 (0)(2)(2) = 0 (1)(0)(0) = 0 (1)(0)(1) = 0 (1)(0)(2) = 0 (1)(1)(0) = 0 (1)(1)(1) = 0 (1)(1)(2) = 0 (1)(2)(0) = 0 (1)(2)(1) = 0 (1)(2)(2) = 0 (2)(0)(0) = 0 (2)(0)(1) = 0 (2)(0)(2) = 0 (2)(1)(0) = 0 (2)(1)(1) = 0 (2)(1)(2) = 0 (2)(2)(0) = 0 (2)(2)(1) = 0 (2)(2)(2) = 0