public class A{
public static void main(String[] args){
int[][] array2d = new int[3][3];
/* array2d[0][0] =0;
array2d[0][1] =1;
array2d[0][2] =2;
array2d[1][0] =3;
array2d[1][1] =4;
array2d[1][2] =5;
array2d[2][0] =6;
array2d[2][1] =7;
array2d[2][2] =8;
*/
int number = 2;
for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
array2d[row][col]= number;
number++;
System.out.print(number + " ");
}
System.out.println();
}
//遍历二维元素,输出每个元素的值
/* System.out.println("二维数组中的元素为: ");
for (int row=0;row<3 ;row++){
for(int col=0;col<3;col++){
System.out.print(number + " ");
}
System.out.println();//换行
}*/
System.out.println("\n使用for-each遍历二维数组中的元素:");
for(int[] arrayElement : array2d){
for(int element : arrayElement){
System.out.print(element + " ");
}
System.out.println();
}
}
public static void main(String[] args){
int[][] array2d = new int[3][3];
/* array2d[0][0] =0;
array2d[0][1] =1;
array2d[0][2] =2;
array2d[1][0] =3;
array2d[1][1] =4;
array2d[1][2] =5;
array2d[2][0] =6;
array2d[2][1] =7;
array2d[2][2] =8;
*/
int number = 2;
for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
array2d[row][col]= number;
number++;
System.out.print(number + " ");
}
System.out.println();
}
//遍历二维元素,输出每个元素的值
/* System.out.println("二维数组中的元素为: ");
for (int row=0;row<3 ;row++){
for(int col=0;col<3;col++){
System.out.print(number + " ");
}
System.out.println();//换行
}*/
System.out.println("\n使用for-each遍历二维数组中的元素:");
for(int[] arrayElement : array2d){
for(int element : arrayElement){
System.out.print(element + " ");
}
System.out.println();
}
}
}
输出结果如下: