Simple to use flexible arrays and multidimensional arrays

Variable-length array defined
Scala> Val arr4 an ArrayBuffer = Int
arr4: scala.collection.mutable.ArrayBuffer [Int] = an ArrayBuffer ()

Printout array
Scala> the println (arr4)
an ArrayBuffer ()

Add elements to the array
Scala> + = arr4. 1
res29: an ArrayBuffer arr4.type = (. 1)

Adding a plurality of array elements to
Scala> arr4 + = (4,2,3,6,5)
res30: an ArrayBuffer arr4.type = (. 1,. 4, 2,. 3,. 6,. 5)

Add the variable is not set array variable
Scala> arr4 the Array ++ = (8,7)
Res 31: arr4.type an ArrayBuffer = (. 1,. 4, 2,. 3,. 6,. 5,. 8,. 7)

Adding an array variable in the variable array
Scala> arr4 an ArrayBuffer ++ = (10,9)
RES32: an ArrayBuffer arr4.type = (. 1,. 4, 2,. 3,. 6,. 5,. 8,. 7, 10,. 9)

Add elements to the array of the specified position
scala> arr4.insert (3, -1,0)

Printout array
Scala> the println (arr4)
an ArrayBuffer (. 1,. 4, 2, -1, 0,. 3,. 6,. 5,. 8,. 7, 10,. 9)

To delete an array element specifies the location
scala> arr4.remove (8,2)

Printing array
Scala> the println (arr4)
an ArrayBuffer (. 1,. 4, 2, -1, 0,. 3,. 6,. 5, 10,. 9)
to delete the specified position array element
scala> arr4.remove (3,2)

scala> println(arr4)
ArrayBuffer(1, 4, 2, 3, 6, 5, 10, 9)

Was added to array location specified array
scala> arr4.insert (4,8,7)

scala> println(arr4)
ArrayBuffer(1, 4, 2, 3, 8, 7, 6, 5, 10, 9)

A method loops through the array
Scala> for (A <- arr4) Print (A)
142 387 659

Method two loop through the array
Scala> for (A <- (0-to arr4.length. 1) .reverse) Print (arr4 (A))
956 783 241

Creates a non-variable group
Scala> Val ARR = the Array (1,2,3,4,5,6,7)
ARR: the Array [Int] = the Array (. 1, 2,. 3,. 4,. 5,. 6,. 7)

yield keyword converting the original array will generate a new array, the array unchanged original
Scala> Val for RES = (A <- ARR) * 2 A yield
RES: the Array [Int] = the Array (2,. 4, 6, 8, 10, 12, 14)

map array conversion
Scala> arr.map (_ * 2)
res47: the Array [Int] = the Array (2,. 4,. 6,. 8, 10, 12 is, 14)

scala> res.map(_ * 2)
res52: Array[Int] = Array(4, 8, 12, 16, 20, 24, 28)

Creates a non-variable group
Scala> Val ARR = the Array (1,2,3,4,5,6)
ARR: the Array [Int] = the Array (. 1, 2,. 3,. 4,. 5,. 6)

                                 ^

Query by the conditions and by array conversion yield
Scala> = var RES for (A <- ARR 2% IF A == 0) yield 10 A *
RES: the Array [Int] = the Array (20 is, 40, 60)

Printout array
Scala> the println (res.toBuffer)
an ArrayBuffer (20 is, 40, 60)

Conditional output array map conversion
Scala> = R & lt Val arr.filter ( % 2 == 0) .map ( * 10)
R & lt: the Array [Int] = the Array (20 is, 40, 60)

scala> println(r.toBuffer)
ArrayBuffer(20, 40, 60)

Summing array
Scala> arr.sum
res56: Int = 21 is

Maximum array
Scala> arr.max
res57: Int. 6 =

Array minimum
Scala> arr.min
res58: Int. 1 =

Add separators
Scala> arr.mkString ( ",")
res62: String = 1,2,3,4,5,6

Creating a multidimensional array
Scala> var mA = the Array (the Array (l, 2,3), the Array (2,3,4))
mA: the Array [the Array [Int]] = the Array (the Array (. 1, 2,. 3), the Array (2, 3, 4))

Query a multidimensional array
Scala> mA (0) (. 1)
res63: Int = 2

Loop through multidimensional array
Scala> for (A <- mA) the println (a.mkString ( ","))
l, 2,3
2,3,4

Guess you like

Origin blog.csdn.net/weixin_44701192/article/details/91475908