主题:二维数组打印

二维数组,以2行3列的数组array[2][3]为例。array数组的长度为行的长度2,每个行元素又是一个一维数组。列数可以为空,行数不可以为空。array[][2]会产生空指针异常。

方法一:for循环 代码:

public static void main(String[] args) {

int[][]array={{1,2,3},{4,5,6}};

for(int row=0;row

for(int col=0;col

System.out.print(array[row][col]+" ");

}

System.out.println();

}

}

方法二:for-each方法 代码:

public static void main(String[] args) {

int[][]array={{1,2,3},{4,5,6}};

for(int[] arr:array){

for(int x:arr){

System.out.print(x+" ");

}

System.out.println();

}

方法三:使用Arrays.deepToString(array)方法打印。 代码:

public static void main(String[] args) {

int[][]array={{1,2,3},{4,5,6}};

System.out.println(Arrays.toString(array));//打印的是地址

System.out.println(Arrays.deepToString(array));

}


世界杯抽签结果出炉!英法荷上上签,德西死亡之组,C罗遇苦主
如何选择适合你宠物的狗狗项圈?(探讨狗狗项圈的类型、功能与尺寸,让你更好的选购适合你的爱犬。)