Easy Tutorial
❮ Scala Strings Home ❯

Scala Arrays

In the Scala language, arrays are used to store a fixed number of elements of the same type. Arrays are one of the important data structures for every programming language.

Declaring an array variable is not about declaring individual variables like number0, number1, ..., number99, but declaring a variable like numbers, and then using numbers[0], numbers[1], ..., numbers[99] to represent individual variables. A specific element in an array is accessed through its index.

The index of the first element of the array is 0, and the index of the last element is one less than the total number of elements.


Declaring Arrays

The following is the syntax for declaring an array in Scala:

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, which can store 3 elements. We can set a value for each element and access each element through its index, as shown below:

z(0) = "tutorialpro"; 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("tutorialpro", "Baidu", "Google")

The figure below shows an array myList with a length of 10, with index values ranging from 0 to 9:


Handling Arrays

The element type and size of an array are determined, so when processing array elements, we usually use a basic for loop.

The following example demonstrates the process of creating and initializing an array:

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 <- 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);
    
   }
}

Executing the above code, the output results are:

$ scalac Test.scala
$ scala Test
1.9
2.9
3.4
3.5
The sum is 11.7
The maximum value is 3.5

Multidimensional Arrays

A multidimensional array is an array whose values can be another array, and the values of another array can also be an array. Matrices and tables 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, each containing three values.

Next, let's look at a complete example of handling 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();
      }
    
   }
}

Executing the above code, the output results are:

$ scalac Test.scala
$ scala Test
0 1 2
0 1 2
0 1 2

Merging Arrays

In the following example, we use the concat() method to merge two arrays. The concat() method accepts multiple array parameters:

Example

``` import Array._

object Test

❮ Scala Strings Home ❯