你如何正确地在java中附加两个2D数组?

时间:2011-04-28 15:16:07

标签: java arrays append

我一直试图在java中附加两个2 D数组。是否有可能得到一个例子,因为我一直试图查找它但找不到它。

int [][]appendArray(empty,window)
{
    int [][]result= new int [empty.length][empty[0].length+window[0].length];       
}

5 个答案:

答案 0 :(得分:7)

你走了:

import java.util.Arrays;


public class Array2DAppend {

    public static void main(String[] args) {

        int[][] a = new int[][] {{1, 2}, {3, 4}};
        int[][] b = new int[][] {{1, 2, 3}, {3, 4, 5}};

        System.out.println(Arrays.deepToString(a));
        System.out.println(Arrays.deepToString(b));
        System.out.println(Arrays.deepToString(append(a, b)));

    }

    public static int[][] append(int[][] a, int[][] b) {
        int[][] result = new int[a.length + b.length][];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }
}

和输出:

[[1, 2], [3, 4]]
[[1, 2, 3], [3, 4, 5]]
[[1, 2], [3, 4], [1, 2, 3], [3, 4, 5]]

答案 1 :(得分:1)

如果我已正确理解您的问题,此方法会将两个2d数组附加在一起 ​​-

private static int[][] appendArrays(int[][] array1, int[][] array2) {
    int[][] ret = new int[array1.length + array2.length][];
    int i = 0;
    for (;i<array1.length;i++) {
        ret[i] = array1[i];
    }
    for (int j = 0;j<array2.length;j++) {
        ret[i++] = array2[j];
    }
    return ret;
}

这段快速代码将测试它 -

        int[][] array1 = new int[][] {
            {1, 2, 3},
            {3, 4, 5, 6},
    };
    int[][] array2 = new int[][] {
            {11, 12, 13},
            {13, 14, 15, 16},
    };

    int[][] expected = new int[][] {
            {1, 2, 3},
            {3, 4, 5, 6},
            {11, 12, 13},
            {13, 14, 15, 16},
    };


    int[][] appended = appendArrays(array1, array2);
    System.out.println("This");
    for (int i = 0; i < appended.length; i++) {
        for (int j = 0; j < appended[i].length; j++) {
            System.out.print(appended[i][j]+", ");
        }
        System.out.println();
    }
    System.out.println("Should be the same as this");
    for (int i = 0; i < expected.length; i++) {
        for (int j = 0; j < expected[i].length; j++) {
            System.out.print(expected[i][j]+", ");
        }
        System.out.println();
    }

答案 2 :(得分:1)

如果我理解正确的话,你想要在相反的维度上追加它,而不是DomS和MeBigFatGuy的想法。如果我是对的,有两种方法:


如果在每个阵列中固定“列”高度(第二维的长度),则可以使用此方法。如果数组具有不同的第一维长度,则会留下空白(零填充)单元格。为了使这段代码更安全,你可能想要

/**
 * For fixed "column" height. "Blank cells" will be left, if the two arrays have different "width" 
 */
static int[][] appendArray2dFix(int[][] array1, int[][] array2){
    int a = array1[0].length, b = array2[0].length;

    int[][] result = new int[Math.max(array1.length,array2.length)][a+b];

    //append the rows, where both arrays have information
    int i;
    for (i = 0; i < array1.length && i < array2.length; i++) {
        if(array1[i].length != a || array2[i].length != b){
            throw new IllegalArgumentException("Column height doesn't match at index: " + i);
        }
        System.arraycopy(array1[i], 0, result[i], 0, a);
        System.arraycopy(array2[i], 0, result[i], a, b);
    }

    //Fill out the rest
    //only one of the following loops will actually run.
    for (; i < array1.length; i++) {
        if(array1[i].length != a){
            throw new IllegalArgumentException("Column height doesn't match at index: " + i);
        }
        System.arraycopy(array1[i], 0, result[i], 0, a);
    }

    for (; i < array2.length; i++) {
        if(array2[i].length != b){
            throw new IllegalArgumentException("Column height doesn't match at index: " + i);
        }
        System.arraycopy(array2[i], 0, result[i], a, b);
    }

    return result;
}

如果要允许列在每个数组中变化,这是可能的,只需稍作更改即可。这不会留下任何空单元格。

/**
 * For variable "column" height. No "blank cells"
 */
static int[][] appendArray2dVar(int[][] array1, int[][] array2){

    int[][] result = new int[Math.max(array1.length,array2.length)][];

    //append the rows, where both arrays have information
    int i;
    for (i = 0; i < array1.length && i < array2.length; i++) {
        result[i] = new int[array1[i].length+array2[i].length];
        System.arraycopy(array1[i], 0, result[i], 0, array1[i].length);
        System.arraycopy(array2[i], 0, result[i], array1[i].length, array2[i].length);
    }

    //Fill out the rest
    //only one of the following loops will actually run.
    for (; i < array1.length; i++) {
        result[i] = new int[array1[i].length];
        System.arraycopy(array1[i], 0, result[i], 0, array1[i].length);
    }

    for (; i < array2.length; i++) {
        result[i] = new int[array2[i].length];
        System.arraycopy(array2[i], 0, result[i], 0, array2[i].length);
    }

    return result;
}

从DomS修改的测试代码

public static void main(String[] args) {

    //Test Var

    int[][] array1 = new int[][] {
            {1, 2, 3},
            {3, 4, 5, 6},
    };
    int[][] array2 = new int[][] {
            {11, 12, 13,14 },
            {13, 14, 15, 16, 17},
    };

    int[][] expected = new int[][] {
            {1, 2, 3, 11, 12, 13, 14},
            {3, 4, 5, 6, 13, 14, 15, 16, 17}
    };


    int[][] appended = appendArray2dVar(array1, array2);
    System.out.println("This");
    for (int i = 0; i < appended.length; i++) {
        for (int j = 0; j < appended[i].length; j++) {
            System.out.print(appended[i][j]+", ");
        }
        System.out.println();
    }
    System.out.println("Should be the same as this");
    for (int i = 0; i < expected.length; i++) {
        for (int j = 0; j < expected[i].length; j++) {
            System.out.print(expected[i][j]+", ");
        }
        System.out.println();
    }


    //Test Fix
    array1 = new int[][] {
            {1, 2, 3, 4},
            {3, 4, 5, 6},
    };
    array2 = new int[][] {
            {11, 12, 13},
            {13, 14, 15},
    };

   expected = new int[][] {
            {1, 2, 3, 4,11, 12, 13},
            {3, 4, 5, 6, 13, 14, 15}
    };


    appended = appendArray2dFix(array1, array2);
    System.out.println("This");
    for (int i = 0; i < appended.length; i++) {
        for (int j = 0; j < appended[i].length; j++) {
            System.out.print(appended[i][j]+", ");
        }
        System.out.println();
    }
    System.out.println("Should be the same as this");
    for (int i = 0; i < expected.length; i++) {
        for (int j = 0; j < expected[i].length; j++) {
            System.out.print(expected[i][j]+", ");
        }
        System.out.println();
    }

}

答案 3 :(得分:0)

我猜是通过“追加”你的意思是用另一行的行来扩展矩阵的行数?在这种情况下,2个数组/矩阵必须具有相同的列数! 因此,例如你可以用b [100] [6]附加[7] [6],这将导致数组c [107] [6],只需将b的100行附加到7行 - 但这只是因为它们两个都有6列。例如用b [100] [6]附加[7] [3]是没有意义的! 因此,您的功能必须预先强制执行这些功能。 不,如果不编写自己的Java,就没有办法在Java中这样做:

int[][] appendArray( empty, window ) {
 if( empty[0].length != window[0].length ) throw new IllegalArgumentException( "Wrong column size" );
 int[][] result = new int[empty.length + window.length];
 for( int i = 0; i < empty.length; i++ )
  System.arrayCopy( empty[i], 0, result[0], 0, empty[i].length );
 for( int i = 0; i < window.length; i++ )
  System.arrayCopy( window[i], 0, result[i + empty.length], 0, window[i].length );
 return result;
}

答案 4 :(得分:0)

单线流

int[][] combi = Stream.concat( Arrays.stream( a ), Arrays.stream( b ) ).toArray( int[][]::new );

二维对象数组here的相应问题