简体   繁体   English

您如何正确 append java 中的两个二维数组?

[英]How do you append two 2D array in java properly?

I have been trying to append two 2 D arrays in java.我一直在尝试 java 中的 append 两个 2 D arrays。 Is it possible to get an example because I have been trying to look it up but cannot find one.是否可以举个例子,因为我一直在尝试查找但找不到。

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

Here you go:这里是 go:

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;
    }
}

and the output:和 output:

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

If I've understood your problem correctly, this method will append two 2d arrays together -如果我正确理解了您的问题,此方法将 append 两个 2d arrays 一起 -

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;
}

This quick bit of code will test it -这段快速的代码将对其进行测试-

        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();
    }

If I understand you correctly, you want to append it, in the opposite dimension, than DomS and MeBigFatGuy thinks.如果我理解正确的话,你想要 append 它,在相反的维度上,比 DomS 和 MeBigFatGuy 认为的。 If I'm is correct there are two ways:如果我是正确的,有两种方法:


If the "column" height (length of 2nd dimension) are fixed within each array you can use this method.如果“列”高度(第二维的长度)在每个数组中是固定的,则可以使用此方法。 It leaves blank (zero-filled) cells, if the arrays have different length of the first dimension.如果 arrays 的第一维长度不同,它将留下空白(零填充)单元格。 Tho make this code safer, you might want to为了让这段代码更安全,你可能想要

/**
 * 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;
}

If you want to allow the column with to vary within each array, this is possible, with a minor change.如果您希望允许列在每个数组中变化,这是可能的,只需稍作更改。 This doesn't leave any empty cells.这不会留下任何空单元格。

/**
 * 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;
}

Test code modified from DomS从 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();
    }

}

I'm guessing by "appending" you mean to extend the number of rows of a matrix with the rows of another?我猜“附加”是指用另一个矩阵的行扩展矩阵的行数? In which case the 2 array / matrices have to have the same number of columns .在这种情况下,2 个数组/矩阵必须具有相同的列数 So for instance you can append a[7][6] with b[100][6] which will result in array c[107][6] by simply appending b's 100 rows to a's 7 rows -- but that's only because they both have 6 columns.因此,例如,您可以 append a[7][6] 和 b[100][6] 这将通过简单地将 b 的 100 行附加到 a 的 7 行来生成数组 c[107][6] ——但这仅仅是因为它们两者都有 6 列。 It doesn't make sense for instance to append a[7][3] with b[100][6], So your function has to enforce these upfront: And no, there is no way of doing this in Java without writing your own which will be something like this:例如 append a[7][3] 和 b[100][6] 是没有意义的,所以你的 function 必须预先强制执行这些:不,没有办法在 ZD52387880E1EAZ223 中执行此操作拥有这将是这样的:

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;
}

one-liner with streams单线带溪流

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

corresponding question for two-dimensional Object arrays here二维 Object arrays 的相应问题

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

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