Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167
Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116
Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180
Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181 Java 如何将小阵列复制到大阵列?_Java - Fatal编程技术网
Java 如何将小阵列复制到大阵列?,java,Java,我想将旧数组复制到新数组中,并将空元素添加到新数组中
import java.util.Arrays;
public class testArray2D {
public static void main(String[] args) {
int[][] a = {{1, 2, 3}};
// make a one bigger
int add = 3;
a = Arrays.copyOf(a, a.length + a
我想将旧数组复制到新数组中,并将空元素添加到新数组中
import java.util.Arrays;
public class testArray2D {
public static void main(String[] args) {
int[][] a = {{1, 2, 3}};
// make a one bigger
int add = 3;
a = Arrays.copyOf(a, a.length + add);
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = 1;
}
}
for (int i[] : a) {
System.out.println(Arrays.toString(i));
}
}
}
为什么无法运行此操作?您需要初始化新的数组元素
import java.util.Arrays;
public class testArray2D {
public static void main(String[] args) {
int[][] a = {{1, 2, 3}};
// make a one bigger
int add = 3;
a = Arrays.copyOf(a, a.length + add);
for (int i = 1; i < a.length; ++i) {
if(a[i] == null) {
a[i] = new int[3];
}
for (int j = 0; j < a[i].length; ++j) {
a[i][j] = 1;
}
}
for (int i[] : a) {
System.out.println(Arrays.toString(i));
}
}
}
import java.util.Arrays;
public class testArray2D {
public static void main(String[] args) {
int[][] a = {{1, 2, 3}};
// make a one bigger
int add = 3;
a = Arrays.copyOf(a, a.length + add);
for (int i = 1; i < a.length; ++i) {
if(a[i] == null) {
a[i] = new int[3];
}
for (int j = 0; j < a[i].length; ++j) {
a[i][j] = 1;
}
}
for (int i[] : a) {
System.out.println(Arrays.toString(i));
}
}
}
import java.util.Arrays;
public class TestArray2D {
public static void main(String[] args) {
int[][] a = { { 1, 2, 3 } };
// Create a backup of the original array
int[][] backup = a;
int add = 3;
// Increase the size of the array by the value of add
a = new int[a.length + add][a[0].length];
// Copy the backup into the enlarged array
System.arraycopy(backup, 0, a, 0, backup.length);
// Fill the remaining values in the enlarged array with 1
for (int i = backup.length; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = 1;
}
}
// Print the enlarged array
for (int i[] : a) {
System.out.println(Arrays.toString(i));
}
}
}
您也可以这样做:
import java.util.Arrays;
public class TestArray2D {
public static void main(String[] args) {
int[][] a = { { 1, 2, 3 } };
// Create a backup of the original array
int[][] backup = a;
int add = 3;
// Increase the size of the array by the value of add
a = new int[a.length + add][a[0].length];
// Copy the backup into the enlarged array
System.arraycopy(backup, 0, a, 0, backup.length);
// Fill the remaining values in the enlarged array with 1
for (int i = backup.length; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = 1;
}
}
// Print the enlarged array
for (int i[] : a) {
System.out.println(Arrays.toString(i));
}
}
}