1

Array is divided into variable array ArrayBuffer and immutable array Array.

Array

Array is an immutable array. Immutable here means that the reference of the object cannot be changed. Its length is fixed and cannot be changed, but its elements can be changed.
Define an immutable array:

// 长度为3的数字,默认都是0,[]里是类型,()里是长度
val arr1: Array[Int] = new Array[Int](3)
// 直接定义数组的元素,这里就是直接调用apply方法了
val arr2: Array[Int] = Array(1, 2, 3)

Read and modify:

println(arr1(0)) // 0
println(arr2(0)) // 1
arr1(0) = 1
arr2.update(0, 11)
println(arr1(0)) // 1
println(arr2(0)) // 11

Convert to a string, and use-to connect:

println(arr2.mkString("-")) // 1-2-3

Find the maximum, minimum, and sum:

println(arr2.sum) // 16
println(arr2.max) // 11
println(arr2.min) // 2

Add elements to the front or back. Since it is an immutable array, the new array is returned.

  • .+:() : method, plus sign in front, add elements in front
  • .:+() : method, plus sign at the back, so add elements at the back
  • +: : symbol, plus sign in front, element is added in front
  • :+ : symbol, plus sign at the back, so add elements at the back
val arr3 = arr2.+:(10).:+(4)
println(arr3.mkString("-")) // 10-11-2-3-4

val arr4 = 8 +: 9 +: arr3 :+ 5 :+ 6
println(arr4.mkString("-")) // 8-9-10-11-2-3-4-5-6

Traverse the array:
until, to, indexes are to take the index, and then take the value through the index. The difference between until and to is that to is equal to the length of the array until is less than.
The next few are to directly take the elements of the array.

for (i <- 0 until arr2.length) print(arr2(i) + "-") // 11-2-3-
println()
for (i <- 0 to arr2.length - 1) print(arr2(i) + "-") // 11-2-3-
println()
for (i <- arr2.indices) print(arr2(i) + "-") // 11-2-3-
println()
for (i <- arr2) print(i + "-") // 11-2-3-
println()
arr2.foreach((e: Int) => print(e + "-")) // 11-2-3-
println()
arr2.foreach(println) // 11 2 3

ArrayBuffer

ArrayBuffer is a variable array, so it can be added, deleted and other operations.

// 长度为3的数字,默认空,[]里是类型,()里是长度
var arr1: ArrayBuffer[Int] = new ArrayBuffer()
var arr2: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
println(arr1) // ArrayBuffer()
println(arr2) // ArrayBuffer(1, 2, 3)

Read, modify, this is different from immutable arrays, because there is no initial value, so the assigned value cannot be read.

// println(arr1(0)) // error
// arr1(0) = 1 // error

println(arr2(0)) // 1
arr2(0) = 11
println(arr2(0)) // 11
arr2.update(0, 1)
println(arr2(0)) // 

Convert it to a string and use-concatenate to find the maximum value, minimum value, and sum to traverse the array. These are the same as immutable arrays, so I won’t demonstrate them here.
Add elements forward or backward, including methods and symbols:

arr2.append(4)
println(arr2.mkString("-")) // 1-2-3-4
arr2.appendAll(ArrayBuffer(5, 6))
println(arr2.mkString("-")) // 1-2-3-4-5-6
arr2.prepend(99)
println(arr2.mkString("-")) // 99-1-2-3-4-5-6
arr2.prependAll(ArrayBuffer(97, 98))
println(arr2.mkString("-")) // 97-98-99-1-2-3-4-5-6

arr2 += 7
println(arr2.mkString("-")) // 97-98-99-1-2-3-4-5-7
arr2 += (8, 9)
println(arr2.mkString("-")) // 97-98-99-1-2-3-4-5-6-7-8-9
arr2 ++= ArrayBuffer(10, 11)
println(arr2.mkString("-")) // 97-98-99-1-2-3-4-5-6-7-8-9-10-11


arr2 :+= 12
println(arr2.mkString("-")) // 97-98-99-1-2-3-4-5-6-7-8-9-10-11
arr2 +:= 96
println(arr2.mkString("-")) // 96-97-98-99-1-2-3-4-5-6-7-8-9-10-11

New element at specified location:

arr2.insert(4, 100)
println(arr2.mkString("-")) // 96-97-98-99-100-1-2-3-4-5-6-7-8-9-10-11-12
arr2.insertAll(5, ArrayBuffer(101, 102))
println(arr2.mkString("-")) // 96-97-98-99-100-101-102-1-2-3-4-5-6-7-8-9-10-11-12

To delete an element, you can specify the position, specify the position + delete length, and delete the specified element:

arr2.remove(4)
println(arr2.mkString("-")) // 96-97-98-99-101-102-1-2-3-4-5-6-7-8-9-10-11-12
arr2.remove(4, 2)
println(arr2.mkString("-")) // 96-97-98-99-1-2-3-4-5-6-7-8-9-10-11-12
arr2 -= 96
println(arr2.mkString("-")) // 97-98-99-1-2-3-4-5-6-7-8-9-10-11-12
arr2 -= (97, 98, 99)
println(arr2.mkString("-")) // 1-2-3-4-5-6-7-8-9-10-11-12

Conversion

Immutable arrays and variable arrays are directly convertible. For example, in the following example, arr3 is of type Array, which is converted to arr4 of Buffer through the toBuffer method, and arr4 is converted to arr5 of Array through the toArray method.

val arr3 = new Array[Int](4)
val arr4: mutable.Buffer[Int] = arr3.toBuffer
val arr5: Array[Int] = arr4.toArray

大军
847 声望183 粉丝

学而不思则罔,思而不学则殆


下一篇 »
Scala - 序列

引用和评论

2 篇内容引用
0 条评论