Scala Arrays
Arrays in Scala store a fixed-size sequence of elements of the same type; arrays are one of the important data structures in every programming language.
Declaring an array variable does not declare separate variables number0, number1, ..., number99; instead you declare a single variable like numbers and then use numbers[0], numbers[1], ..., numbers[99] to represent the individual variables. A specific element in an array is accessed by index.
The first element of an array has index 0, and the last element has an index equal to the number of elements minus 1.
Declaring Arrays
The following is the syntax for declaring an array in Scala:
var z:Array[String] = new Array[String](3)
或
var z = new Array[String](3)
In the syntax above, z declares an array of strings with length 3, able to store 3 elements. We can set a value for each element and access each element by index, as shown below:
z(0) = "Runoob"; 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 define an array in the following way:
var z = Array("Runoob", "Baidu", "Google")
The figure below shows an array myList of length 10, with index values from 0 to 9:

Processing Arrays
An array's element type and size are both fixed, so when processing array elements we usually use a basic for loop.
The following example demonstrates array creation, initialization, and other processing:
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
myList
=
Array
(
1.9
,
2.9
,
3.4
,
3.5
)
// Print all array elements
for
(
x
<
)
{
println
(
x
)
}// Compute the sum of all array elements
var
total
=
0.0
;
for
(
i
<
0
to
(myList.
length
1
)
)
{
total +
=
myList
(
i
)
;
}
println
(
"The sum is "
- total
)
;
// Find the largest 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
)
;
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
1.9
2.9
3.4
3.5
总和为 11.7
最大值为 3.5
Multidimensional Arrays
Multidimensional arrays: the values in one array can be another array, and the values of that array can be yet another array. Matrices and tables are common two-dimensional arrays.
The above is an example that defines a two-dimensional array:
val myMatrix = Array.ofDim[Int](3, 3)
In the example, the array contains three array elements, each of which in turn contains three values.
Next, let's look at a complete example of processing a two-dimensional array:
Example
import
Array.
_
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
val
myMatrix
=
Array.
ofDim
[
Int
]
(
3
,
3
)
// Create a matrix
for
(
i
<
0
to
2
)
{
for
(
j
<
0
to
2
)
{
myMatrix
(
i
)
(
j
)
=
j;
}
}
// Print the two-dimensional array
for
(
i
<
0
to
2
)
{
for
(
j
<
0
to
2
)
{
print
(" "
- myMatrix
(
i
)
(
j
)
)
;
}
println
(
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
0 1 2
0 1 2
0 1 2
Concatenate arrays
In the following example, we use the concat() method to merge two arrays; concat() accepts multiple array arguments:
Example
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
)
// Print all array elements
for
(
x
<
)
{
println
(
x
)
}
}
}The output of the code above 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 over a range. The last parameter of range() is the step, which defaults to 1:
Example
import
Array.
_
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
myList1
=
range
(
10
,
20
,
2
)
var
myList2
=
range
(
10
,
20
)
// Print all array elements
for
(
x
<
)
{
print
(" "
- x
)
}
println
(
)
for
(
x
<
)
{
print
(" "
- x
)
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
10 12 14 16 18
10 11 12 13 14 15 16 17 18 19
Scala Array Methods
The table below lists important methods for working with arrays in Scala; before using them we need to use import Array._ to import the package.
| No. | Method and Description |
|---|---|
| 1 |
def apply( x: T, xs: T* ): Array[T] Create an array of the specified object T, where T can be Unit, Double, Float, Long, Int, Char, Short, Byte, or Boolean. |
| 2 |
def concat[T]( xss: Array[T]* ): Array[T] Concatenate arrays |
| 3 |
def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit Copy one array to another. Equivalent to Java's System.arraycopy(src, srcPos, dest, destPos, length). |
| 4 |
def empty[T]: Array[T] Return an array of length 0 |
| 5 |
def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T] Return an array of the specified length, where each element is the return value of the given function. In the example above, the array is initialized to 0, has length 3, and the computation function isa=>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]
Return an array whose length is specified by the first parameter, with each element filled using the second parameter.
| | 7 |def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]
Return a two-dimensional array whose length is specified by the first parameter, with each element filled using the second parameter.
| | 8 |def ofDim[T]( n1: Int ): Array[T]
Create an array of the 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 over the specified range, where step is the interval between elements
| | 12 |def range( start: Int, end: Int ): Array[Int]
Create an array over the specified range
| | 13 |def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]
Returns an array of the specified length, where each element is the return value of the specified function, starting from 0 by default.
The above example returns 3 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 the specified length, where each element is the return value of the specified function, starting from 0 by default.
|