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,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));
        }
    }
}
导入java.util.array;
公共类testArray2D{
公共静态void main(字符串[]args){
int[]a={{1,2,3};
//再大一点
int-add=3;
a=数组.copyOf(a,a.length+add);
对于(int i=1;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));
        }
    }
}
导入java.util.array;
公共类testArray2D{
公共静态void main(字符串[]args){
int[]a={{1,2,3};
//再大一点
int-add=3;
a=数组.copyOf(a,a.length+add);
对于(int i=1;i
如果要将现有阵列复制到比现有阵列大的新阵列,我建议您执行以下操作

int[][] source_arr = {{1, 2, 3}};
int add = 3;
int[][] dest_arr = new int[source_arr[0].length+add][source_arr[0].length];
for (int i = 0; i < source_arr.length; i++) {
    System.arraycopy(source_arr[i], 0, dest_arr [i], 0, source_arr[i].length);
}
int[]source_arr={{{1,2,3};
int-add=3;
int[]dest_arr=new int[source_arr[0].length+add][source_arr[0].length];
for(int i=0;i
如果要将现有阵列复制到比现有阵列大的新阵列,我建议您执行以下操作

int[][] source_arr = {{1, 2, 3}};
int add = 3;
int[][] dest_arr = new int[source_arr[0].length+add][source_arr[0].length];
for (int i = 0; i < source_arr.length; i++) {
    System.arraycopy(source_arr[i], 0, dest_arr [i], 0, source_arr[i].length);
}
int[]source_arr={{{1,2,3};
int-add=3;
int[]dest_arr=new int[source_arr[0].length+add][source_arr[0].length];
for(int i=0;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));
        }
    }
}

您会遇到什么类型的错误?请好奇,在
copyOf
之后打印数组(或使用调试器查看其内容)(提示java中没有真正的2D数组,它是数组数组数组)
数组。copyOf
将填充空值,而不是第0个插槽中的重复实例。它是否编译?如果是,将给出答案。您会遇到什么类型的错误?请好奇,在
copyOf
之后打印数组(或使用调试器查看其内容)(提示java中没有真正的2D数组,它是数组数组数组)
数组。copyOf
将填充空值,不是在第0个插槽中的重复实例。是否编译?如果有,我会给你答案。