How do you add two 2D arrays in java correctly?

I am trying to add two 2 D arrays to java. Is it possible to get an example because I tried to find it but cannot find it.

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

      

+1


source to share


4 answers


Here you 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 is:

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

      

+7


source


If I understand you correctly, you want to add it in the opposite dimension than DomS and MeBigFatGuy think. If I am correct there are two ways:




If the column height (2nd dimension length) is fixed within each array, you can use this method. It leaves empty (zero filled) cells if the arrays have different lengths of the first dimension. To make this code more secure, you might want

/**
 * 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 the column to change in every array, this is possible with minor modifications. This leaves no blank 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

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

}

      

+1


source


I am assuming that "adding" means increasing the number of rows of a matrix with rows of another? In this case, the 2 arrays / matrices must have the same number of columns ! For example, you can add [7] [6] with b [100] [6], which will result in an array c [107] [6], just adding b 100 lines to 7 lines - but that's only because they are both have 6 columns. For example, it makes no sense to add [7] [3] with b [100] [6]! Thus, your function must enforce these requirements. And no, there is no way to do this in Java without writing your own, which would be something like this:

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

      

0


source


If I understood your problem correctly, this method will add two 2d arrays together -

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

      

0


source







All Articles