17.多维数组
多维数组
1.一维数组
int[] arr1 = {1,2,3};
System.out.println(arr1[0]);
2.二维数组
//[4][2],四行二列
/*
1,1 arr2[0]
2,2 arr2[1]
3,3 arr2[2]
4,4 arr2[3]
*/
int[][] arr2 = {{1,1},{2,2},{3,3},{4,4}};
printArray(arr2[0]);
System.out.println(arr2[0][1]);
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i] + " ");
}
}
二维数组的不同维度元素打印方式不同。
3.二维数组的遍历
int[][] arr2 = {{1,1},{2,2},{3,3},{4,4}};
for (int i = 0; i < arr2.length; i++) { //外层
for (int j = 0; j < arr2[i].length; j++) { //内层
System.out.print(arr2[i][j] + " ");
}
}