轉發(fā)請注明出處:
安卓猴的博客(http://sunjiajia.com)
本節(jié)課程將學習以下內容:
- 數(shù)組的類型
- 數(shù)組的定義方法
- 數(shù)組的操作方法
數(shù)組的類型
數(shù)組的定義方法
數(shù)組的操作方法
例子1:
class Demo01 {
public static void main(String[] args) {
// 數(shù)組的靜態(tài)定義法
// arr是一個整型數(shù)組
int[] arr = { 2, 5, 6, 7, 8, 4, 0 };
// 取數(shù)組中的一個值:arr[下標],下標從0開始。
System.out.println(arr[3]);
// 打印數(shù)組中所有的元素
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// 數(shù)組的動態(tài)聲明法,10表示數(shù)組的長度為10
// arr2中的元素的值默認為0
int[] arr2 = new int [10];
// 二維數(shù)組的定義方法
int[][] arr3 = {{ 1, 5, 6}, { 5, 8, 9}, { 2, 5}};
System.out.println(arr[1][1]);
for (int i = 0; i < arr3.length; i++) {
for (int j = 0; j < arr3[i].length; j++) {
System.out.println(arr[i][j]);
}
}
}
}