public class MultiDimen {
public static void main(String[] arg) {
int firstArray[][] = { { 8, 9, 19, 11 }, { 12, 13, 14, 15 }, };
int secondArray[][] = { { 30, 31, 32, 33 }, { 43 }, { 4, 5, 6, }, };
System.out.println("This is the first Array");
display(firstArray);
System.out.println("This is the second Array");
display(secondArray);
}
public static void display(int x[][]) {
for (int row = 0; row < x.length; row++) {
System.out.println("ROW:" + x[row].length + "[row].length");
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}
}
我理解这里发生了什么,然而,有一点不清楚的是x [row] .length的作用是什么? 我知道x.length获取传入的colomns数组x的长度。但是row不是一个数组,它被声明为int所以我们为什么要这样做呢?
答案 0 :(得分:2)
使用您自己的代码的示例将最好地解释x[row].length
正在做什么:
int secondArray[][]={
{30,31,32,33},
{43},
{4,5,6,},
};
for (int row=0; row < secondArray.length; ++row) {
System.out.println("Row " + (row+1) + " has " + secondArray[row].length + " elements.");
}
row[i].length
生成包含在2D int[]
数组的第i个位置的1D int[][]
数组中的元素数。
<强>输出:强>
Row 1 has 4 elements.
Row 2 has 1 elements.
Row 3 has 3 elements.
答案 1 :(得分:0)
int numOfRows = matrix.length;
int numOfCols = matrix[0].length;