English 中文(简体)
Scala Collections - Array
  • 时间:2024-12-22

Scala Collections - Array


Previous Page Next Page  

Scala provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring inspanidual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent inspanidual variables. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. The index of the first element of an array is the number zero and the index of the last element is the total number of elements minus one.

Declaring Array Variables

To use an array in a program, you must declare a variable to reference the array and you must specify the type of array the variable can reference.

The following is the syntax for declaring an array variable.

Syntax


var z:Array[String] = new Array[String](3)

or

var z = new Array[String](3)

Here, z is declared as an array of Strings that may hold up to three elements. Values can be assigned to inspanidual elements or get access to inspanidual elements, it can be done by using commands pke the following −

Command


z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan"

Here, the last example shows that in general the index can be any expression that yields a whole number. There is one more way of defining an array −


var z = Array("Zara", "Nuha", "Ayan")

Following picture represents an array myList. Here, myList holds ten double values and the indices are from 0 to 9.

Scala Array

Processing Arrays

When processing array elements, we often use loop contol structures because all of the elements in an array are of the same type and the size of the array is known.

Below is an example program of showing how to create, initiapze and process arrays −

Example


object Demo {
   def main(args: Array[String]) {
      var myList = Array(1.9, 2.9, 3.4, 3.5)
      
      // Print all the array elements
      for ( x <- myList ) {
         println( x )
      }
      // Summing all elements
      var total = 0.0;
      for ( i <- 0 to (myList.length - 1)) {
         total &plus;= myList(i);
      }
      println("Total is " + total);
      // Finding the largest element
      var max = myList(0);
      for ( i <- 1 to (myList.length - 1) ) {
         if (myList(i) > max) max = myList(i);
      }
      println("Max is " &plus; max);
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command


>scalac Demo.scala
>scala Demo

Output


1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

Scala does not directly support various array operations and provides various methods to process arrays in any dimension. If you want to use the different methods then it is required to import Array._ package.

Advertisements