-------
android培训、
java培训、期待与您交流! ----------
public class Print2Arrays {
/**
* 二维数组的打印和遍历
*/
public static void main(String[] args) {
int[][] arr = { { 133, 12, 5, 11 }, { 4, 33, 122, 14 } };
printArrays(arr);
}
public static void printArrays(int[][] array) {
// 遍历二维数组中的第一个数组
// 其实可以把二维数组看成是一个数组里面的元素是一维数组
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print("[");
// 遍历一组数组里面的小数组
for (int j = 0; j < array[i].length; j++) {
if (j < array[i].length - 1) { // 如果当前角标小于一维数组中的一维数组中的长度-1
System.out.print(array[i][j] + ",");
} else
System.out.print(array[i][j]);
}
if (i != array.length - 1) { // 如果没有到一维数组长度-1就打印],如果到了就打印]
System.out.print("],");
} else {
System.out.print("]");
}
}
System.out.println("}");
}
}