Arrays are divided into a mutable Array ArrayBuffer and an immutable Array.

# Array

Array is an immutable Array. By immutable, we mean that the reference to an object cannot be changed. Its length is fixed and cannot be changed, but its elements can be changed. Define an immutable array:

``Var arr1: Array[Int] = new Array[Int](3) var arr2: Array[Int] = new Array[Int](3) var arr2: Array[Int] = new Array[Int](3) Array[Int] = Array(1, 2, 3)``

``````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 concatenate with – :

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

To find a maximum, a minimum, a sum:

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

Adding an element before or after an immutable array returns a new array.

• `+ : ().`: method with the plus sign in front, so always add the element before
• `. : + ()`: method with the plus sign after it, so always add an element after it
• `+ :`: Symbol, with the plus sign before it, so the element is added before it
• `: +`: symbol, followed by the plus sign, so always add an element after it
``````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``````

Indices: until, to, indices are used to select index values from indexes. The difference between until and to is that to is equal to the length of the array until is less than. The last couple of them are just 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 mutable array, so it can be added, deleted, and so on.

``Var arr1: arrayBuffer [Int] = new arrayBuffer () var arr2: arrayBuffer [Int] = new arrayBuffer () var arr2: arrayBuffer () ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) println(arr1) // ArrayBuffer() println(arr2) // ArrayBuffer(1, 2, 3)``

This is different from an immutable array in that it cannot read an assigned value because it has no initial value.

``````// 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)) // ``````

Converts to strings, uses -concatenation, maxima, min, summation to iterate over arrays, which are immutable arrays, which I won’t demonstrate here. Add elements forward or backward, including both 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 in the 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 location, location + deletion length, or element deletion:

``````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 array and mutable array are directly convertible. For example, in the following example, arr3 is of type Array and is converted to arr4 of Buffer by the toBuffer method, and arr4 is converted to arr5 of Array by the toArray method

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