Home > Software engineering >  Java add array to array special mode
Java add array to array special mode

Time:10-07

I am working on a game in Java and I need to cache some position-based information. If I have the following array:

int[][] array = {
            {1, 2, 3},
            {1, 2, 3},
            {1, 2, 3}
    };

And then I have this other array:

int[][] otherArray = {
            {4, 5, 6},
            {4, 5, 6},
            {4, 5, 6}
    };

Now I want to combine them in a special way. I want to add otherArray to the left of array. So the result would look like this:

int[][] combinedArray = {
            {4, 5, 6, 1, 2, 3},
            {4, 5, 6, 1, 2, 3},
            {4, 5, 6, 1, 2, 3}
    };

Then I have this other combined array:

int[][] otherCombinedArray = {
            {30, 17, 139, 65, 335, 99},
            {50, 43, 57, 53, 423, 534},
            {90, 67, 78, 24, 99, 67}
    };

Now I want to add it to the top of the original combined array. So the final result would look like this:

int[][] finalCombinedArray = {
            {30, 17, 139, 65, 335, 99},
            {50, 43, 57, 53, 423, 534},
            {90, 67, 78, 24, 99, 67},
            {4, 5, 6, 1, 2, 3},
            {4, 5, 6, 1, 2, 3},
            {4, 5, 6, 1, 2, 3}
    };

Can someone point me to a good library or built in method for doing this? I also wanted to note that the method shouldn't be too computationally heavy (like looping through all the arrays multiple times, and it shouldn't use too much memory, like 80MB).

Thank you for the help!

CodePudding user response:

On it's own, Java does not provide concatenation methods, but we can use System.arraycopy (as suggested by @user16320675 above)

With System.arraycopy you specify the array to copy the destination array -> you have array A of size 10 and B of size 2, and you use the command to copy your B array into A.

  int[] source = { 1,2,3,4,5 };
  int[] destination = new int[10];

  // You can play with the numbers below
  int COPY_AT_INDEX = 0;  // Start to copy at position destination[0]
  int AMOUNT_TO_COPY = 5; // Copying from 0 to source.length

  System.arraycopy(source, 0, destination, COPY_AT_INDEX, AMOUNT_TO_COPY);

  System.out.println(Arrays.toString(source));  // [1, 2, 3, 4, 5]
  System.out.println(Arrays.toString(destination));  // [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

Now, if we use arraycopy, seems you have to determine when to copy as rows, and when to copy as columns.

// Assuming a[][] and b[][] have the same size.
public int[][] mergeAsColumns(int[][] a, int[][] b) {
    int rows = a.length;
    int columns = a[0].length;

    int[][] merged = new int[rows][2 * columns];

    for (int i = 0; i < a.length; i  ) {
        System.arraycopy(a[i], 0, merged[i], 0, columns);
        System.arraycopy(b[i], 0, merged[i], rows, columns);
    }

    return merged;
}

Merging as Rows is similar to the other one, but changes in which positions you want to affect and how you create the merged array.

// Assuming a[][] and b[][] have the same size.
public int[][] mergeAsRows(int[][] a, int[][] b) {
    int rows = a.length;
    int columns = a[0].length;

    int[][] merged = new int[2 * rows][columns];

    for (int i = 0; i < rows; i  ) {
        System.arraycopy(a[i], 0, merged[i], 0, columns);
        System.arraycopy(b[i], 0, merged[rows   i], 0, columns);
    }

    return merged;
}
  • Related