Latest web development tutorials

Scala array

Array Scala language provided is used to store a fixed size elements of the same type, one for each gate array Editors language, are important data structures.

Declaration of an array variable is not declared number0, number1, ..., number99 a separate variable, but a declaration like this variable numbers, then use the numbers [0], numbers [1], ..., numbers [ 99] to represent a separate variable. An array element specified by the index is accessed.

The first element of the array index 0, the last element's index is the number of elements minus one.


Declare an array

The following is the syntax Scala array declaration:

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

或

var z = new Array[String](3)

The above syntax, z declare a string type of array, the array length is 3, can store three elements. We can set the value for each element, and access each element by index, as follows:

z(0) = "w3big"; z(1) = "Baidu"; z(4/2) = "Google"

Index of the last element in the expression using4/2 as an index, similar to z (2) = "Google".

We can also define an array using the following methods:

var z = Array("w3big", "Baidu", "Google")

The following figure shows an array of length myList 10, the index value of 0-9:


Working with arrays

Type and size of the array element of the array is determined, so when processing array elements, we often use the basic for loop.

The following example demonstrates the creation of an array, initialization process:

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

Implementation of the above code, the output is:

$ scalac Test.scala
$ scala Test
1.9
2.9
3.4
3.5
总和为 11.7
最大值为 3.5

Multidimensional Arrays

A multidimensional array values ​​in the array can be another array, another array value can also be an array. Matrix and our common form is a two-dimensional array.

The above is an example of a defined two-dimensional array:

var myMatrix = ofDim[Int](3,3)

Examples of the array contains three elements of an array, each array element in turn contains three values.

Next, we look at a complete example of a two-dimensional array of 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();
      }
    
   }
}

Running instance »

Implementation of the above code, the output is:

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

Merge array

The following examples, we use concat () method to merge two arrays, 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 )
      }
   }
}

Implementation of the above code, the output is:

$ scalac Test.scala
$ scala Test
1.9
2.9
3.4
3.5
8.9
7.9
0.4
1.5

Create an array interval

The following examples, we use the range () method to produce the array within a wide range. range () method last parameter in steps of 1 by default:

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

Implementation of the above code, the output 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 following table is an important method for processing language Scala array, before using it we need to useimport Array._ introduced package.

No. Method and Description
1

def apply (x: T, xs : T *): Array [T]

Create an array of the specified object T, the value of T may be Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean.

2

def concat [T] (xss: Array [T] *): Array [T]

Merge array

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 is

5

def iterate [T] (start: T, len: Int) (f: (T) => T): Array [T]

Returns the length of the array, each array element is the return value of the specified function.

Examples of the above array initial value of 0 and a length of 3 to calculate the functiona => 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, the first parameter is specified, and each element of the second argument to fill length.

7

def fill [T] (n1: Int, n2: Int) (elem: => T): Array [Array [T]]

Returns two arrays, the first parameter is specified, and each element of the second argument to fill length.

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 the specified range, step increments 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 the length of the array, each array element is the return value of the specified function, from zero by default.

The above examples return three 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, each array element is the return value of the specified function, from zero by default.