I am not a quitter.
使用陣列時,必須宣告一個變數來引用陣列,同時確定陣列變數的型別,如以下語法:
var z:Array[String] = new Array[String](3)
or
var z = new Array[String](3)
用如下方式賦值:z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan"
或者定義並初始化:var z = Array("Zara", "Nuha", "Ayan")
2 使用陣列
object Test { 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 += 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 " + max); } }
結果如下:
C:/>scalac Test.scala
C:/>scala Test
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
3 多維陣列
多維陣列定義如下:
import Array._ object Test { def main(args: Array[String]) { var myMatrix = ofDim[Int](3,3) // build a 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(); } } }
結果如下:
C:/>scalac Test.scala
C:/>scala Test
0 1 2
0 1 2
0 1 2
4 合併多個數組
使用contact()方法,合併為一個大陣列
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 the array elements for ( x <- myList3 ) { println( x ) } } }
結果如下:
C:/>scalac Test.scala
C:/>scala Test
1.9
2.9
3.4
3.5
8.9
7.9
0.4
1.5
5 用Range建立陣列
import Array._
object Test {
def main(args: Array[String]) {
var myList1 = range(10, 20, 2)
var myList2 = range(10,20)
// Print all the array elements
for ( x <- myList1 ) {
print( " " + x )
}
println()
for ( x <- myList2 ) {
print( " " + x )
}
}
}
結果如下:C:/>scalac Test.scala
C:/>scala Test
10 12 14 16 18
10 11 12 13 14 15 16 17 18 19
6 Scala陣列的方法
1 def apply( x: T, xs: T* ): Array[T]
Creates an array of T objects, where T can be Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean.
2 def concat[T]( xss: Array[T]* ): Array[T]Concatenates all arrays into a single array.
3 def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): UnitCopy one array to another. 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 containing repeated applications of a function to a start value.
6 def fill[T]( n: Int )(elem: => T): Array[T]
Returns an array that contains the results of some element computation a number of times.
7 def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]Returns a two-dimensional array that contains the results of some element computation a number of times.
8 def iterate[T]( start: T, len: Int)( f: (T) => T ): Array[T]Returns an array containing repeated applications of a function to a start value.
9 def ofDim[T]( n1: Int ): Array[T]
Creates array with given dimensions.
10 def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]]Creates a 2-dimensional array
11 def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]]Creates a 3-dimensional array
12 def range( start: Int, end: Int, step: Int ): Array[Int]Returns an array containing equally spaced values in some integer interval.
13 def range( start: Int, end: Int ): Array[Int]Returns an array containing a sequence of increasing integers in a range.
14 def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]Returns an array containing values of a given function over a range of integer values starting from 0.
15 def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]Returns a two-dimensional array containing values of a given function over ranges of integer values starting from 0.