1. Array definition
Fixed Array length
ArrayBuffer variable length
If you do not use new when providing the initial value, the bottom layer will directly call the apply method
-
Constant Array
Val A1 = array (element 1, element 2,... )We don't need new. The underlying calls apply() to help us with new.
var a2 = new Array [element type] (array length) -
Variable length array
import scala.collection.mutable.ArrayBuffer is required before use.
Val A3 = array (element 1, element 2,... )
var a4 = new Array [element type]
Add elements:
a1 + = element
a1 + = (element 1, element 2,...) )
a1. + = (element)
A1.append (element)
a1 + + = array
A1.insert (subscript, element)
Delete element:
a1 - = element
A1.remove (element angle) -
Traversing array
for cycle
until loop.reverse
map .filter(...).map(...) -
Array common methods
arr.sum
arr.max
arr.min
arr.sorted
arr.sorted.reverse
arr.toMap dual Array to Map
Zipper operation
If the number of elements of the two arrays is inconsistent, the length of the array generated after the zipper operation is the number of elements of the smaller array.
arr1.zipAll(arr2, default... )If the number of one element is small, you can use zipAll to fill it with the default element.
2. demo
- Fixed length array
scala> val a1 = new Array[String](4) a1: Array[String] = Array(null, null, null, null) scala> a1(1) = "hello" scala> a1 res5: Array[String] = Array(null, hello, null, null) scala> val a2 = Array("hello","world") a2: Array[String] = Array(hello, world) scala> a2(0) res6: String = hello scala> a2(0)="hi" scala> a2 res8: Array[String] = Array(hi, world)
Variable length array
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> var a3 = new ArrayBuffer[Int] a3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> var a4 = ArrayBuffer(1,2) a4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2) scala> a4 += 3 res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> a4.append(4) scala> a4 res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4) scala> a4 -= 2 res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 4) scala> a4.remove(1) res13: Int = 3 scala> a4 res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 4)
scala> val a1 = new Array[Int](3) a1: Array[Int] = Array(0, 0, 0) scala> println(a1) [I@2c01480f scala> println(a1.toBuffer) ArrayBuffer(0, 0, 0) scala> val a2 = new ArrayBuffer[Int]() a2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> a2 += 1 res43: a2.type = ArrayBuffer(1) scala> a2 += (2,3,4) res44: a2.type = ArrayBuffer(1, 2, 3, 4) scala> a2.append(5) scala> a2 res46: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5) scala> a2.+=(6) res47: a2.type = ArrayBuffer(1, 2, 3, 4, 5, 6) scala> a2 ++= Array(7,8) res48: a2.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8) scala> a2 ++= ArrayBuffer(9,10) res49: a2.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> a2.insert(2,11) scala> a2 res51: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 11, 3, 4, 5, 6, 7, 8, 9, 10) //To insert an element in an array, insert it from a subscript. scala> a2.insert(3,12,13) scala> a2 res53: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 11, 12, 13, 3, 4, 5, 6, 7, 8, 9, 10) scala> a2 -= 11 res54: a2.type = ArrayBuffer(1, 2, 12, 13, 3, 4, 5, 6, 7, 8, 9, 10) //remove the element at a certain position of the array, and delete it according to the subscript scala> a2.remove(2) res55: Int = 12 scala> a2 res56: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 13, 3, 4, 5, 6, 7, 8, 9, 10)
- Traversing array
scala> a4 res24: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 4) scala> for(i <- a4) println(i) 1 4 scala> for(i <- 0 until a4.size) println(a4(i)) 1 4 reverse take until Generated Range Reversal scala> for(i <- (0 until a4.length).reverse) println(a4(i)) 4 1 scala> val a = a4.map(i => println(i)) 1 4 a: scala.collection.mutable.ArrayBuffer[Unit] = ArrayBuffer((), ()) scala> val a = a4.map(println(_)) 1 4 a: scala.collection.mutable.ArrayBuffer[Unit] = ArrayBuffer((), ()) scala> val a = a4.map(i => i*10) a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 40) scala> val a = a4.map(_*10) a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 40) scala> val a5 = for(i <- a4) yield i*2 a5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 8) filterYes filter, receive a return value of boolean Function mapIt is equivalent to taking out every element in the array and applying the function passed in. scala> val a6 = a5.filter(_%4==0).map(_*10) a6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(80)
- Array common methods
scala> a5 res78: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 8) scala> a5.sum res73: Int = 10 scala> a5.max res74: Int = 8 scala> a5.min res75: Int = 2 scala> a5.sorted res76: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 8) scala> a5.sorted.reverse res77: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8, 2) scala> val a7 = Array(("name","zsy"),("age",20)) a7: Array[(String, Any)] = Array((name,zsy), (age,20)) scala> a7.toMap res79: scala.collection.immutable.Map[String,Any] = Map(name -> zsy, age -> 20) scala> val names = Array("zs","ls","ww") names: Array[String] = Array(zs, ls, ww) scala> val scores = Array(90,85,88) scores: Array[Int] = Array(90, 85, 88) scala> names.zip(scores) res80: Array[(String, Int)] = Array((zs,90), (ls,85), (ww,88)) scala> val scores = Array(90,85) scores: Array[Int] = Array(90, 85) scala> names.zip(scores) res81: Array[(String, Int)] = Array((zs,90), (ls,85)) scala> names.zipAll(scores) <console>:15: error: not enough arguments for method zipAll: (that: scala.collection.GenIterable[B], thisElem: A1, thatElem: B)(implicit bf: scala.collection.generic.CanBuildFrom[Array[String],(A1, B),That])That. Unspecified value parameters thisElem, thatElem. names.zipAll(scores) ^ scala> names.zipAll(scores,"zs",80) res83: Array[(String, Int)] = Array((zs,90), (ls,85), (ww,80))