Java 数组代码如何在楼梯模型中工作? int[]sel=newint[4][]; int i,j; for(i=0;i
创建一个数组数组(读取->数组指针数组),大小为4Java 数组代码如何在楼梯模型中工作? int[]sel=newint[4][]; int i,j; for(i=0;i,java,arrays,for-loop,multidimensional-array,nested-loops,Java,Arrays,For Loop,Multidimensional Array,Nested Loops,创建一个数组数组(读取->数组指针数组),大小为4 int [][] sel = new int [4][]; int i, j; for (i=0; i<sel.length; i++) sel [i] = new int [i+1]; for (i=0; i<sel.length; i++) for (j=0; j<sel[i].length; j++) sel [i][j] = i+j;
int [][] sel = new int [4][];
int i, j;
for (i=0; i<sel.length; i++)
sel [i] = new int [i+1];
for (i=0; i<sel.length; i++)
for (j=0; j<sel[i].length; j++)
sel [i][j] = i+j;
for (i=0; i<sel.length; i++) {
for (j=0; j<sel[i].length; j++)
System.out.print(sel[i][j] + " ");
System.out.println();
}
声明索引
int [][] sel = new int [4][];
循环指针数组并将指针设置为长度索引为+1的数组
int i, j;
for(Java中的i=0;i数组是对象,因此具有未知长度或可变长度的多维数组的每个索引都必须手动初始化(或者直接使用并省去您自己的麻烦!)基本上,编译器将生成一部分代码来初始化任何具有隐式大小的数组,就像基本数组一样
for (i=0; i<sel.length; i++) {
for (j=0; j<sel[i].length; j++)
System.out.print(sel[i][j] + " ");
System.out.println();
}
后续维度为“索引+1”更大,编译器无法知道这一点,因此创建它们的第一个循环。第二个循环填充创建的第二个维度,这实际上是一个可怕的例子,因为一个适当的程序员知道要避免像瘟疫一样的迭代;相反,他/她应该立即在第一个循环中填充数组,如下所示:
[4]
for(int i=0;i
这样做有几个原因,主要原因是不需要测试逻辑或多次迭代整个数组进行初始化……但是,最好区分初始化和函数,因此打印值的第二个循环应该保留
资料来源:
在二维数组中,它的工作方式类似于行和列
在这行代码中,您声明了一个int 2d数组变量sel
,其中有4行和一个空列
for (int i = 0; i < sel.length; i++) {
final int size = i+1;
final int[] dimension = sel[i] = new int[size];
for(int j = 0; j < size; j++) {
dimension[j] = i+j;
}
}
这两个变量用于循环的内部
int [][] sel = new int [4][];
在这个for循环中,您将为sel
的索引分配一个数组
int i, j;
//长度=4
对于(i=0;i请找到问题的解释
// length = 4
for (i=0; i<sel.length; i++)
//first iteration sel[0] = new int[i+1] 0 + 1 = 1 size of array inside sel[0]
//second iteration sel[1] = new int[i+1] 1 + 1 = 2 size of array inside sel[1]
// and so on
sel [i] = new int [i+1];
//assigning to the value of `sel` 's column
// length = 4
for (i=0; i<sel.length; i++)
// sel[i] or sel[0].length = 1 refer to the first for loop
// sel[1].length = 2
for (j=0; j<sel[i].length; j++)
//sel[0][0] = 0 + 0;
//sel[1][0] = 1 + 0;
// sel[1][1] = 1 + 1;
sel [i][j] = i+j;
// displaying the values
//length = 4
for (i=0; i<sel.length; i++) {
// sel[i] or sel[0].length = 1 refer to the first for loop
// sel[1].length = 2
for (j=0; j<sel[i].length; j++)
//sel[0][0] = 0;
//sel[1][0] = 1;
//sel[1][1] = 2;
System.out.print(sel[i][j] + " ");
System.out.println();
publicstaticvoidmain(字符串[]args)
{
/*Java从许多一维数组构建多维数组,即所谓的“数组的数组”方法。
*这有几个有趣的结果:行的大小可能不同。
*每一行都是一个可以独立使用的对象(数组)。
*您已经初始化了一个具有固定行数和所有行中可变列数的数组。
*这使我们能够灵活地向每行动态添加“n”个列。
*如果声明为int[4][4],则始终只能向每行添加4列。
*下面的示例将在下面的结构中初始化多维数组。
* +-----+
|a[0]|->空值
| |
+-----+
| |
|a[1]|->空值|
+-----+
依此类推直到a[3]。注意,a[n]的值为空或null,因为我们没有给出数组的大小。
因此,访问sel[0][0]将导致null指针异常,sel[0]将导致null指针异常。
*
*/
int[][]sel=新int[4][];
int i,j;
/*
*现在sel.length将给出矩阵中的行数(多维数组)。
*因此,所有四个数组(每行)都将由下面的for循环使用一个数组进行初始化。
*在for循环之后,printing sel[i]将打印数组的哈希代码。
*在这种情况下,,
*第一行将是大小为1的数组
*第二排:2
*第三行:3,依此类推。默认情况下,所有元素都将初始化为零。
* +-----+ +-----+
|a[0]|->|0|
| | +-----+
+-----+
| | +-----+-----+
|a[1]|->|0 | 0 |
+-----+ +-----+-----+
*
*/
对于(i=0;i 2,3->3,4->4
*/
对于(i=0;i您需要更具体。您到底不明白什么?(如果您对代码的格式也更仔细一些,这会有所帮助。)抱歉,但我是根据java书中的例子写的,我不理解,因为数组输出像楼梯。第一行有4个变量,第二行有3个变量等等。注意,如果for构造缺少{scope body},它将只应用于下一行-对于初学者来说,这可能会让人困惑。
int i, j;
// length = 4
for (i=0; i<sel.length; i++)
//first iteration sel[0] = new int[i+1] 0 + 1 = 1 size of array inside sel[0]
//second iteration sel[1] = new int[i+1] 1 + 1 = 2 size of array inside sel[1]
// and so on
sel [i] = new int [i+1];
//assigning to the value of `sel` 's column
// length = 4
for (i=0; i<sel.length; i++)
// sel[i] or sel[0].length = 1 refer to the first for loop
// sel[1].length = 2
for (j=0; j<sel[i].length; j++)
//sel[0][0] = 0 + 0;
//sel[1][0] = 1 + 0;
// sel[1][1] = 1 + 1;
sel [i][j] = i+j;
// displaying the values
//length = 4
for (i=0; i<sel.length; i++) {
// sel[i] or sel[0].length = 1 refer to the first for loop
// sel[1].length = 2
for (j=0; j<sel[i].length; j++)
//sel[0][0] = 0;
//sel[1][0] = 1;
//sel[1][1] = 2;
System.out.print(sel[i][j] + " ");
System.out.println();
public static void main(String[] args)
{
/* Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach.
* There are a couple of interesting consequences of this: Rows may be different sizes.
* Each row is an object (an array) that can be used independently.
* You have initialized an Array with fixed number of rows, and variable number of columns in all the rows.
* This gives us felxibility to dynamically add 'n' number of columns to each row.
* if you had declared it say, int[4][4], you could always only add 4 columns to each row.
* The below example would initialize a multidimensional array in the below structure.
* +-----+
|a[0] | -> null
| |
+-----+
| |
|a[1] | -> null |
+-----+
and so on till a[3]. Note the value for a[n] are empty or null since we have not given the size of the arrays.
So accessing sel[0][0] would give you null pointer Exception and sel[0] would give you null.
*
*/
int [][] sel = new int [4][];
int i, j;
/*
* Now sel.length would give us the number of rows in the matrix(multi-dimensional array).
* So all the four arrays(each row) would be initialized with an array by the following for loop.
* After the for loop, printing sel[i] would print the hashcode of the array.
* In this case,
* 1st row will be an array of size: 1
* 2nd row: 2
* 3rd row: 3 and so on. and all the elements will be initialized to zero by default.
* +-----+ +-----+
|a[0] | -> | 0 |
| | +-----+
+-----+
| | +-----+-----+
|a[1] | -> | 0 | 0 |
+-----+ +-----+-----+
*
*/
for (i=0; i<sel.length; i++)
sel [i] = new int [i+1];
/*
* The below code does nothing but iterates the above created matrix and assigns them a value.
* sel.length will always be 4.
* sel[i].length for 1st row will be:1, 2-> 2,3->3, 4->4
*/
for (i=0; i<sel.length; i++)
for (j=0; j<sel[i].length; j++)
sel [i][j] = i+j;
/*
* The below code iterates row by row, prints the content of each array in that row and then a new line.
*/
for (i=0; i<sel.length; i++) {
for (j=0; j<sel[i].length; j++)
System.out.print(sel[i][j] + " ");
System.out.println();
}