在不使用 Java 数组 class 的情况下追加两个二维数组
Append two 2D arrays without use of the Java arrays class
我很困惑如何制作一个 java 方法来附加两个不同的二维数组,而不 using/importing 数组 class。这是我目前所拥有的:
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
}
for (int i = 0; i < second.length; i++) {
for (int j = 0; j < second[i].length; j++) {
// don't know what to do here
}
}
return third;
}
Cell 只是一个对象,但我正在尝试了解附加数组背后的逻辑,因此我们将不胜感激!
此外,我知道这里有一个类似的问题 (How do you append two 2D array in java properly?),但答案是从上到下附加数组(假定两个参数数组具有相同数量的列),而我正在寻找左右附加数组(假设两个参数数组具有相同的行数)。
你快到了。我想这就是您要找的东西。
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
for (int j = first[i].length; j < first[i].length + second[i].length; j++) {
third[i][j] = second[i][j-first[i].length];
}
}
return third;
}
我很困惑如何制作一个 java 方法来附加两个不同的二维数组,而不 using/importing 数组 class。这是我目前所拥有的:
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
}
for (int i = 0; i < second.length; i++) {
for (int j = 0; j < second[i].length; j++) {
// don't know what to do here
}
}
return third;
}
Cell 只是一个对象,但我正在尝试了解附加数组背后的逻辑,因此我们将不胜感激!
此外,我知道这里有一个类似的问题 (How do you append two 2D array in java properly?),但答案是从上到下附加数组(假定两个参数数组具有相同数量的列),而我正在寻找左右附加数组(假设两个参数数组具有相同的行数)。
你快到了。我想这就是您要找的东西。
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
for (int j = first[i].length; j < first[i].length + second[i].length; j++) {
third[i][j] = second[i][j-first[i].length];
}
}
return third;
}