Home > Mobile >  Turning two 2-Dimensional Arrays into one 2-Dimensional Array with different length of Arrays and di
Turning two 2-Dimensional Arrays into one 2-Dimensional Array with different length of Arrays and di

Time:01-06

Hello,

how can i turn two 2D Arrays into one 2D Array. The Array length is different from the Other. And inside a 2D Array the Arrays also can have a different length.

Example:

int[][] ary2DFirst = new int[][]{{1,1,1,1},{2,2,2},{3,3,3}};
int[][] ary2DSecond = new int[][]{{4},{5,5,5,5,5}};

result should be

addedUpAry2D == {{1,1,1,1},{2,2,2},{3,3,3},{4},{5,5,5,5,5}}

I couldn´t do it jet with a 2D Array, just with a 1D Array (as showed in could below) Thank you

import java.util.Arrays;

public class ArraysAndMore {

    public static int[] add2D(int[][]ary1, int[][]ary2){
    //Attention please. the return must be a two dimensional Array, because i couldnt do it, i did it with a one Dimensional Array.

        int aryElementCountFirst = 0;
        int aryElementCountSecond = 0;

        for (int[] i:ary1) {
            for (int n:i) {
                aryElementCountFirst  ;
            }
        }

        for (int[] i:ary2) {
            for (int n:i) {
                aryElementCountSecond  ;
            }
        }

        int[]arysInOneDFirst = new int[aryElementCountFirst];
        int[]arysInOneDSecond = new int[aryElementCountSecond];
        int counter = 0;

        for (int[] i:ary1) {
            for (int n:i) {
                arysInOneDFirst[counter  ] = n;
            }
        }

        int counter2 = 0;
        for (int[] i:ary2) {
            for (int n:i) {
                arysInOneDSecond[counter2  ] = n;
            }
        }

        int[] aryTwo1Dtogether = add(arysInOneDFirst, arysInOneDSecond);
        return aryTwo1Dtogether; **//should be a 2D Array!!**
    }

    public static int[] add(int[] ary1, int[]ary2) {

        ** //This Method turns two 1D Arrays into one 1D Array**\

        if(ary1.length==0 && ary2.length==0){
            return new int[0];
        }
        int[]addedUp = new int[ary1.length  ary2.length];
        int counter = 0;
        for (int i = 0; i < ary1.length; i  ) {
            addedUp[counter  ] = ary1[i];
        }
        for (int i = 0; i < ary2.length; i  ) {
            addedUp[counter  ] = ary2[i];
        }
        return addedUp;
    }
    
    public static void main(String[] args) {

        int[][] ary2D1 = new int[][]{{1,1,1,1},{2,2,2},{3,3,3}};
        int[][] ary2D2 = new int[][]{{4},{5,5,5,5,5}};

        System.out.println(Arrays.toString(add2D(ary2D1, ary2D2)));
    }
    
}

My Output looks like this:

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

it needs to look like this:

{{1,1,1,1},{2,2,2},{3,3,3},{4},{5,5,5,5,5}}

CodePudding user response:

Try it like this.

  • use Arrays.copyOf to create a new array of the same size and copying the first.
  • then the easiest way is to just use a loop to copy the second array to the first.
  • this ensures that the resultant array does not share objects with the previous two.
public class ArraysAndMore {

    public static void main(String[] args) {
        int[][] ary2DFirst = new int[][]
            {
                    {1,1,1,1},
                    {2,2,2},
                    {3,3,3}};
        int[][] ary2DSecond = new int[][]
            {
                    {4},
                    {5,5,5,5,5}};

        int[][] result = add2D(ary2DFirst, ary2DSecond);
        System.out.println(Arrays.deepToString(result));
    }

   public static int[][] add2D(int[][] ary1, int[][] ary2) {
        // allocate a new array of proper size
        int[][] result = new int[ary1.length   ary2.length][];
        
        // copy the first array
        for (int i = 0; i < ary1.length; i  ) {
            result[i] = Arrays.copyOf(ary1[i], ary1[i].length);
        }
        
        // copy the second array;
        int k = ary1.length;
        for (int i = 0; i < ary2.length; i  ) {
            result[k  ] = Arrays.copyOf(ary2[i], ary2[i].length);
        }
        return result;
    }
}

prints

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

The key to remember is that 2D arrays are just arrays of arrays.

An alternative to accomplish the same thing is to use streams.

int result2[][] = Stream.of(ary2DFirst, ary2DSecond)
                .flatMap(Arrays::stream).map(s -> Arrays.copyOf(s, s.length))
                .toArray(int[][]::new);

The Arrays.copyOf is required to ensure the arrays don't share objects.

CodePudding user response:

You can use System.arraycopy to do this. You do not need to specify the size of the second dimension of the array (which is how you are able to have your original arrays to begin with).

The first copy takes a1 and adds it to a (the resulting array) and then adds a2 to the end of a1 (which is now in in a).

public class so75024045 {
    public static void main(String[] args) {
        int[][] a1 = new int[][] { { 1, 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } };
        int[][] a2 = new int[][] { { 4 }, { 5, 5, 5, 5, 5 } };
        int[][] a = new int[a1.length   a2.length][];

        System.arraycopy(a1, 0, a, 0, a1.length);
        System.arraycopy(a2, 0, a, a1.length, a2.length);

        for (int[] x : a) {
            for (int y : x) {
                System.out.print(y   " ");
            }
            System.out.println("");
        }
    }
}

CodePudding user response:

An alternative simple approach using streams:

public static int[][] add2D(int[][]ary1, int[][]ary2){
    return Stream.concat(Arrays.stream(ary1), Arrays.stream(ary2))
                 .toArray(int[][]::new);
}
  • Related