# 操作陣列物件

`int[] scores = new int[10];`

 型態 初始值 ```byte ``` ```0 ``` `short` ```0 ``` ```int ``` ```0 ``` ```long ``` ```0L ``` ```float ``` ```0.0F ``` ```double ``` ```0.0D ``` ```char ``` ```\u0000（空字元） ``` ```boolean ``` ```false ``` 類別 ```null ```

``````package cc.openhome;

import java.util.Arrays;

public class Score2 {
public static void main(String[] args) {
int[] scores = new int[10];
for(int score : scores) {
System.out.printf("%2d", score);
}
System.out.println();
Arrays.fill(scores, 60);
for(int score : scores) {
System.out.printf("%3d", score);
}
}
}
``````

0 0 0 0 0 0 0 0 0 0
60 60 60 60 60 60 60 60 60 60

```int[] scores = new int[] {88, 81, 74, 68, 78, 76, 77, 85, 95, 93}; ```

```int[] scores1 = {88, 81, 74, 68, 78, 76, 77, 85, 95, 93}; int[] scores2 = scores1; scores2[0] = 99; System.out.println(scores1[0]); ```

`scores2[0] = 99`的意思是，將`scores2`參考的陣列物件索引0指定為99，而顯示時使用`scores1[0]`的意思是，取得`scores1`參考的陣列物件索引0的值，結果就是99。

`int[][] cords = new int[2][3];`

```for(int x = 0; x < cords.length; x++) {     for(int y = 0; y < cords[x].length; y++) {         System.out.printf("%2d", cords[x][y]);     }     System.out.println(); }```

`for(int[] row : cords) {`
`    for(int value : row) {`
`        System.out.printf("%2d", value);`
`    }`
`    System.out.println();`
`}`

`int[][] cords = new int[][] {`
`    {1, 2, 3},`
`    {4, 5, 6}`
`};`

``````package cc.openhome;

public class IrregularArray {
public static void main(String[] args) {
int[][] arr = new int[2][];
arr[0] = new int[] {1, 2, 3, 4, 5};
arr[1] = new int[] {1, 2, 3};
for(int[] row : arr) {
for(int value : row) {
System.out.printf("%2d", value);
}
System.out.println();
}
}
}
``````

1 2 3 4 5
1 2 3

```int[][] arr = {     {1, 2, 3, 4, 5},     {1, 2, 3} }; ```

`Integer[] scores = new Integer[3];`

``````package cc.openhome;
public class IntegerArray {
public static void main(String[] args) {
Integer[] scores = new Integer[3];
for(Integer score : scores) {
System.out.println(score);
}
scores[0] = new Integer(99);
scores[1] = new Integer(87);
scores[2] = new Integer(66);
for(Integer score : scores) {
System.out.println(score);
}
}
}
``````

null
null
null
99
87
66

```scores[0] = 99; scores[1] = 87; scores[2] = 66; ```

`Integer[] scores = {new Integer(99), new Integer(87), new Integer(66)};`

`Integer[] scores = {99, 87, 66};`

`Integer[][] cords = new Integer[3][2];`

`new  Integer[3][2]`代表著一個`Integer[][]`型態的物件，裏頭有3個`Integer[]`型態的索引，分別參考至長度為2的`Integer`一維陣列物件，而每個`Integer`一維陣列的索引都參考至`null`，所以答案還是0個`Integer`實例。