输出二维数组:
public class erwei{
public static void main(String[] args){
int [][]arr={{0,0,0,0,0,0},
{,0,0,1,0,0,0},
{0,2,0,0,3,0},
{0,0,0,0,0,0}};
for(int i=0;i<arr.length;i++){
for (int j=0;j<arr[i].length ;j++ ) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
动态创建二维下面数组并输出
i=0:1
i=1:2 2
i=2:3 3 3
public class erwei{
public static void main(String[] args){
int [][]arr=new int[3][];
for (int i=0;i<arr.length ;i++ ) {
arr[i]=new int[i+1];
for (int j=0;j<arr[i].length ;j++ ) {
arr[i][j]=i+1;
}
}
for (int i=0;i<arr.length ;i++ ) {
for (int j=0;j<arr[i].length ;j++ ){
System.out.print(arr[i][j]+"");
}
System.out.println();
}
}
}
结果:
其中int [][]arr=new int[3][]中可通过修改3来输出不同结果。