考虑:
int[][] multD = new int[5][];
multD[0] = new int[10];
这就是创建5行10列的二维数组的方法吗?
我在网上看到了这段代码,但语法没有意义。
考虑:
int[][] multD = new int[5][];
multD[0] = new int[10];
这就是创建5行10列的二维数组的方法吗?
我在网上看到了这段代码,但语法没有意义。
当前回答
也可以用以下方式声明它。这不是什么好设计,但很管用。
int[] twoDimIntArray[] = new int[5][10];
其他回答
创建5行10列的二维数组最常见的习惯用法是:
int[][] multD = new int[5][10];
或者,你也可以使用下面的代码,它更类似于你所拥有的,尽管你需要显式地初始化每一行:
int[][] multD = new int[5][];
for (int i = 0; i < 5; i++) {
multD[i] = new int[10];
}
int [][] twoDim = new int [5][5];
int a = (twoDim.length);//5
int b = (twoDim[0].length);//5
for(int i = 0; i < a; i++){ // 1 2 3 4 5
for(int j = 0; j <b; j++) { // 1 2 3 4 5
int x = (i+1)*(j+1);
twoDim[i][j] = x;
if (x<10) {
System.out.print(" " + x + " ");
} else {
System.out.print(x + " ");
}
}//end of for J
System.out.println();
}//end of for i
试试这个方法:
int a[][] = {{1,2}, {3,4}};
int b[] = {1, 2, 3, 4};
Try:
int[][] multD = new int[5][10];
注意,在代码中,只有2D数组的第一行被初始化为0。 第二行到第五行根本不存在。如果你试着打印它们,你会得到所有的null。
也可以用以下方式声明它。这不是什么好设计,但很管用。
int[] twoDimIntArray[] = new int[5][10];