Scala array
The arrays provided in the Scala language are used to store fixed-size elements of the same type. Arrays are one of the important data structures for every editing language.
Declaring an array variable does not mean declaring number0, number1,..., number99 as separate variables, but declaring a variable like numbers, and then using numbers[0], numbers[1], ..., numbers[99] to represent individual variables. A specific element in the array is accessed by index.
The index of the first element of the array is 0, and the index of the last element is the total number of elements minus 1.
Declaring arrays
The following is the syntax format of Scala array declaration:
var z:Array[String] = new Array[String](3) 或 var z = new Array[String](3)
In the above syntax, z declares an array of string type, and the array length is 3 , can store 3 elements. We can set the value for each element and access each element by index as follows:
z(0) = "php"; z(1) = "Baidu"; z(4/2) = "Google"
The index of the last element uses the expression 4/2 as the index, Similar to z(2) = "Google".
We can also use the following method to define an array:
var z = Array("php", "Baidu", "Google")
The following figure shows an array myList with a length of 10, with index values from 0 to 9:
Processing arrays
The element type of the array and the size of the array are determined, so when processing array elements, we usually use a basic for loop.
The following example demonstrates the creation, initialization and other processing processes of the array:
object Test { def main(args: Array[String]) { var myList = Array(1.9, 2.9, 3.4, 3.5) // 输出所有数组元素 for ( x <- myList ) { println( x ) } // 计算数组所有元素的总会 var total = 0.0; for ( i <- 0 to (myList.length - 1)) { total += myList(i); } println("总和为 " + total); // 查找数组中的最大元素 var max = myList(0); for ( i <- 1 to (myList.length - 1) ) { if (myList(i) > max) max = myList(i); } println("最大值为 " + max); } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test 1.9 2.9 3.4 3.5 总和为 11.7 最大值为 3.5
Multidimensional array
Multidimensional array The value in one array can be another array, and the value of another array can also be an array. Matrices and tables are our common two-dimensional arrays.
The above is an example that defines a two-dimensional array:
var myMatrix = ofDim[Int](3,3)
In the example, the array contains three array elements, and each array element contains three values.
Next let’s look at a complete example of two-dimensional array processing:
import Array._ object Test { def main(args: Array[String]) { var myMatrix = ofDim[Int](3,3) // 创建矩阵 for (i <- 0 to 2) { for ( j <- 0 to 2) { myMatrix(i)(j) = j; } } // 打印二维阵列 for (i <- 0 to 2) { for ( j <- 0 to 2) { print(" " + myMatrix(i)(j)); } println(); } } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test 0 1 2 0 1 2 0 1 2
Merge arrays
In the following example, we use the concat() method to merge two arrays. The concat() method accepts multiple array parameters:
import Array._ object Test { def main(args: Array[String]) { var myList1 = Array(1.9, 2.9, 3.4, 3.5) var myList2 = Array(8.9, 7.9, 0.4, 1.5) var myList3 = concat( myList1, myList2) // 输出所有数组元素 for ( x <- myList3 ) { println( x ) } } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test 1.9 2.9 3.4 3.5 8.9 7.9 0.4 1.5
Create a range array
In the following example, we use the range() method to generate an array within the range. The last parameter of the range() method is the step size, which defaults to 1:
import Array._ object Test { def main(args: Array[String]) { var myList1 = range(10, 20, 2) var myList2 = range(10,20) // 输出所有数组元素 for ( x <- myList1 ) { print( " " + x ) } println() for ( x <- myList2 ) { print( " " + x ) } } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test 10 12 14 16 18 10 11 12 13 14 15 16 17 18 19
Scala array method
The table shows the important methods for processing arrays in the Scala language. Before using it, we need to use import Array._ to introduce the package.
Serial number | Method and description |
---|---|
1 | def apply( x: T, xs: T* ): Array[T] Create an array of the specified object T. The value of T can be Unit, Double, Float, Long, Int, Char, Short, Byte ,Boolean. |
2 | def concat[T]( xss: Array[T]* ): Array[T] Merge arrays |
##def copy( src: AnyRef, srcPos: Int, dest: AnyRef , destPos: Int, length: Int ): Unit Copy one array to another array. Equivalent to Java's System.arraycopy(src, srcPos, dest, destPos, length). | |
def empty[T]: Array[T] The return length is Array of 0 | |
def iterate[T]( start: T, len: Int )( f: (T ) => T ): Array[T] Returns an array of specified length, and each array element is the return value of the specified function. The initial value of the above example array is 0, the length is 3, and the calculation function is a=>a+1: | |
##def fill[T]( n: Int )(elem: => T): Array[T] | Return array, length Specified for the first parameter, while each element is filled with the second parameter. | 7
def fill[T]( n1: Int, n2: Int )( elem: => T ) : Array[Array[T]] | Returns a two-array array with the length specified by the first parameter, and each element is filled with the second parameter. | 8
def ofDim[T]( n1: Int ): Array[T] | Create an array of specified length | 9
##def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]] | Create a two-dimensional array 10 |
def ofDim [T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]] | Create a three-dimensional array 11 |
def range( start: Int, end: Int, step: Int ): Array[Int] | Create within the specified range Array, step is the step size between each element 12 |
def range( start: Int, end: Int ): Array[Int] | Create an array within the specified range 13 |
def tabulate[T]( n: Int )(f: (Int)=> T): Array[T] | Returns an array of specified length. Each array element is the return value of the specified function. Default starts from 0. The above example returns 3 elements: scala> Array.tabulate(3)(a => a + 5) res0: Array[Int] = Array(5, 6, 7)14 |
Returns a two-dimensional array of the specified length, each array element is the specified The return value of the function starts from 0 by default. |