# Scala數組

Scala提供了一種數據結構叫作數組，數組是一種存儲了相同類型元素的固定大小順序集合。數組用於存儲數據集合，但將數組視爲相同類型變量的集合通常更爲有用。

## 聲明數組變量

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

// 或者

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

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

``var z = Array("Maxsu", "Nancy", "Alen")``

## 處理處理

``````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 += 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);
}
}``````

``````D:\>scalac Demo.scala
D:\>scala Demo

1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5``````

Scala不直接支持各種數組操作，它提供各種方法來處理任何維度的數組。 如果要使用不同的方法，則需要導入`Array._`包。

## 多維數組

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

``````import Array._

object Demo {
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();
}
}
}``````

``````D:\>scalac Demo.scala
D:\>scala Demo

0 1 2
0 1 2
0 1 2``````

## 連接陣列

``````import Array._

object Demo {
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 )
}
}
}``````

``````D:\>scalac Demo.scala
D:\>scala Demo

1.9
2.9
3.4
3.5
8.9
7.9
0.4
1.5``````

## 使用範圍創建數組

``````import Array._

object Demo {
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 )
}
}
}``````

``````D:\>scalac Demo.scala
D:\>scala Demo

10 12 14 16 18
10 11 12 13 14 15 16 17 18 19``````

## Scala數組方法

1

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

2

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

3

`def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit`

4

`def empty[T]: Array[T]`

5

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

6

`def fill[T]( n: Int )(elem: => T): Array[T]`

7

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

8

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

9

`def ofDim[T]( n1: Int ): Array[T]`

10

`def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]]`

11

`def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]]`

12

`def range( start: Int, end: Int, step: Int ): Array[Int]`

13

`def range( start: Int, end: Int ): Array[Int]`

14

`def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]`

`0`開始的整數值範圍內，返回一個包含給定函數值的數組。

15

`def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]`