English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Arrays provided by the Scala language are used to store fixed-size elements of the same type. Arrays are one of the important data structures for every programming language.
Declaring an array variable is not the same as declaring number0, number1, ..., number99 Instead of declaring individual variables, declare a variable like numbers, then use numbers[0], numbers[1]], ..., numbers[99]] to represent individual variables. An element at a specific position in the array is accessed by its 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 minus1.
The syntax format for declaring an array in Scala is as follows:
var z:Array[String] = new Array[String](3) or var z = new Array[String](3)
In the above syntax, z declares an array of string type, with a length of 3 ,可存储 3 elements. We can set a value for each element and access each element by index, as shown below:
z(0) = "w3codebox"; z(1) = "Baidu"; z(4/2) = "Baidu"; z(
) = "Google" 4/2 As an index, similar to z(2) = "Google".
We can also define an array in the following way:
var z = Array("w3codebox", "Baidu", "Google")
The figure below shows an array of length 10 of the array myList, index value 0 to 9:
The element type and size of the array are fixed, so when processing array elements, we usually use a basic for loop.
The following example demonstrates the creation, initialization, and other processing processes of arrays:
object Test { def main(args: Array[String]) { var myList = Array(1.9, 2.9, 3.4, 3.5) // Output all array elements for ( x <- myList ) { println( x ) } // Calculate the sum of all elements in the array var total = 0.0; for ( i <- 0 to (myList.length - 1)) { total += myList(i); } println("The sum is " + total); // Find the maximum element in the array var max = myList(0); for ( i <- 1 to (myList.length - 1) ) { if (myList(i) > max) max = myList(i); } println("The maximum value is " + max); } }
After executing the above code, the output result is:
$ scalac Test.scala $ scala Test 1.9 2.9 3.4 3.5 The sum is 11.7 The maximum value is 3.5
A multi-dimensional array can have an array as a value, and the value of another array can also be an array. Matrix and table are common two-dimensional arrays.
The following is an example of defining a two-dimensional array:
val myMatrix = Array.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 processing a two-dimensional array:
import Array._ object Test { def main(args: Array[String]) { val myMatrix = Array.ofDim[Int](3, 3) // Create matrix for (i <- 0 to 2) { for ( j <- 0 to 2) { myMatrix(i)(j) = j; } } // Print two-dimensional array for (i <- 0 to 2) { for ( j <- 0 to 2) { print(" " + myMatrix(i)(j)); } println(); } } }
After executing the above code, the output result is:
$ scalac Test.scala $ scala Test 0 1 2 0 1 2 0 1 2
In the following example, we use the concat() method to merge two arrays, and 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) // Output all array elements for ( x <- myList3 ) { println( x ) } } }
After executing 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
In the following examples, we used the range() method to generate an array within a range. The last parameter of the range() method is the step, which defaults to 1:
import Array._ object Test { def main(args: Array[String]) { var myList1 = range(10, 20, 2) var myList2 = range(10,20) // Output all array elements for ( x <- myList1 ) { print( " " + x ) } println() for ( x <- myList2 ) { print( " " + x ) } } }
After executing 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
The table below lists important methods for handling arrays in Scala, which we need to use before using them. import Array._ Import packages.
Serial number | Methods and descriptions |
---|---|
1 | def apply( x: T, xs: T* ): Array[T] Creates an array of the specified object T, where T can be Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean. |
2 | def concat[T]( xss: Array[T]* ): Array[T] Merge arrays |
3 | def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit Copies an array to another array. Equivalent to Java's System.arraycopy(src, srcPos, dest, destPos, length). |
4 | def empty[T]: Array[T] Returns an array of length 0 |
5 | def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T] Returns an array of the specified length, with each array element being the return value of the specified function. The initial value of the example array is 0, with a length of 3,计算函数为a=>a+1: scala> Array.iterate(0,3)(a=>a+1) res1: Array[Int] = Array(0, 1, 2) |
6 | def fill[T]( n: Int )(elem: => T): Array[T] Returns an array of the specified length, with each element filled by the second parameter. |
7 | def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]] Returns a two-dimensional array with a length specified by the first parameter, and each element is filled using 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 an array within a specified range, step is the step length between each element |
12 | def range( start: Int, end: Int ): Array[Int] Create an array within a specified range |
13 | def tabulate[T]( n: Int )(f: (Int)=> T): Array[T] Returns an array of specified length, where each array element is the return value of the specified function, default starting from 0. The above example returns 3 Number of elements: scala> Array.tabulate(3)(a => a + 5) res0: Array[Int] = Array(5, 6, 7) |
14 | def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]] Returns a two-dimensional array of specified length, where each array element is the return value of the specified function, default starting from 0. |