简体   繁体   English

将两个二维数组复制到另一个二维数组Java

[英]Copying two bi-dimensional arrays to another bidimensional array Java

I have yet another Java question :) 我还有一个Java问题:)

I have read this thread, where it explains it clearly, but I have two bi-dimensional arrays that I would like to copy. 我已经阅读了这个帖子,它清楚地解释了它,但我有两个我想要复制的二维数组。

I understand that this piece of code 我明白这段代码

int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);

But my question is, how do I merge it with two arrays where 但我的问题是,如何将其与两个数组合并在哪里

int a1[][] = new int [3][3];
int b1[][] = new int [3][3];
int c1[][] = new int [3][6];

Where c1 is the merging of aforementioned arrays? 哪里c1是上述数组的合并?

Use solution from task , that You have mentioned in the question. 使用您在问题中提到的任务解决方案。 Example: 例:

import java.util.Arrays;

public class ArrayProgram {

    public static void main(String[] args) {
        int[][] array1 = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
        int[][] array2 = { { 4, 5, 6 }, { 7, 8, 9 }, { 0, 1, 2 } };
        int[][] result = ArrayCopier.joinSecondDimension(array1, array2);
        for (int[] array : result) {
            System.out.println(Arrays.toString(array));
        }
    }
}

class ArrayCopier {

    public static int[][] joinSecondDimension(int[][] array1, int[][] array2) {
        int[][] array1and2 = new int[array1.length][];
        for (int index = 0; index < array1.length; index++) {
            array1and2[index] = join(array1[index], array2[index]);
        }
        return array1and2;
    }

    public static int[] join(int[] array1, int[] array2) {
        int[] array1and2 = new int[array1.length + array2.length];
        System.arraycopy(array1, 0, array1and2, 0, array1.length);
        System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
        return array1and2;
    }
}

Prints: 打印:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 7, 8, 9]
[1, 2, 3, 0, 1, 2]

EDIT 编辑
Implementation for any arguments number ( Variable-Length Argument Lists ): 任何参数编号的实现( 可变长度参数列表 ):

import java.util.Arrays;

public class ArrayProgram {

    public static void main(String[] args) {
        int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[][] array2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[][] array3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        test(array1);
        test(array1, array2);
        test(array1, array2, array3);
    }

    private static void test(int[][]... arrays) {
        int[][] result = ArrayCopier.joinSecondDimension(arrays);
        for (int[] array : result) {
            System.out.println(Arrays.toString(array));
        }
        System.out.println();
    }
}

class ArrayCopier {

    public static int[][] joinSecondDimension(int[][]... arrays) {
        int firstArrayLength = arrays[0].length;
        int[][] result = new int[firstArrayLength][];
        for (int index = 0; index < firstArrayLength; index++) {
            result[index] = join(getSecondDimArrays(index, arrays));
        }
        return result;
    }

    public static int[] join(int[]... arrays) {
        int[] result = new int[getTotalLength(arrays)];
        int destPos = 0;
        for (int[] array : arrays) {
            System.arraycopy(array, 0, result, destPos, array.length);
            destPos += array.length;
        }
        return result;
    }

    private static int getTotalLength(int[]... arrays) {
        int length = 0;
        for (int[] array : arrays) {
            length += array.length;
        }
        return length;
    }

    private static int[][] getSecondDimArrays(int index, int[][]... arrays) {
        int[][] result = new int[arrays.length][];
        int resultIndex = 0;
        for (int[][] array : arrays) {
            result[resultIndex++] = array[index];
        }
        return result;
    }
}

Prints: 打印:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

[1, 2, 3, 1, 2, 3]
[4, 5, 6, 4, 5, 6]
[7, 8, 9, 7, 8, 9]

[1, 2, 3, 1, 2, 3, 1, 2, 3]
[4, 5, 6, 4, 5, 6, 4, 5, 6]
[7, 8, 9, 7, 8, 9, 7, 8, 9]

Something like this should work perfectly well? 这样的东西应该能很好地运作吗?

int a1[][] = new int [3][3];
int b1[][] = new int [3][3];
int c1[][] = new int [3][6];

for (int i = 0; i < a1.length; i++) {
    System.arraycopy(a1[i], 0, c1[i], 0, a1[i].length);
    System.arraycopy(a2[i], 0, c1[i], a1[i].length, a2[i].length);
}

And by the way, I assumed from your dimensions that c looks like: 顺便说一句,我从你的维度假设c看起来像:

[  a, a, a,  ,  ,  ]
[  a, a, a,  , b,  ]
[  a, a, a,  ,  ,  ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM