7

I have yet another Java question :)

I have read this thread, where it explains it clearly, but I have two bi-dimensional arrays that I would like to copy.

I understand that this piece of code

int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);

But my question is, how do I merge it with two arrays where

int a1[][] = new int [3][3];
int b1[][] = new int [3][3];
int c1[][] = new int [3][6];

Where c1 is the merging of aforementioned arrays?

Community
  • 1
  • 1
Marco Berrocal
  • 362
  • 1
  • 10
  • 4
    Just use `for` loop and use `arraycopy` inside it. Did you try them? – Mikita Belahlazau Nov 25 '12 at 22:04
  • imagine the 2nd dimension of your array is the one dimensional array which you want to combine. So loop over the first dimension and do the stuff which you already found to the second dimension. – timaschew Nov 25 '12 at 22:09
  • Since, there are two dimensions, it's not clear how they should be merged. An example would be good. – Bhesh Gurung Nov 25 '12 at 22:15
  • I have a tough time when it comes to imagining dimensions, etc. But what you guys say makes sense..... – Marco Berrocal Nov 25 '12 at 22:15
  • @BheshGurung I would like to merge them one next to the other. I need 9 arrays of [3][3] dimensions each merged as I am going to develop a Sudoku but would like to have access to individual arrays to verify sums, etc separately. – Marco Berrocal Nov 25 '12 at 22:17
  • 1
    I think it's easier for you to just have one 9X9 array. IMO, shouldn't have to play around with so many arrays. Even if you have them separate, I don't see the need to merge them. – Bhesh Gurung Nov 25 '12 at 22:19

2 Answers2

3

Use solution from task, that You have mentioned in the question. Example:

import java.util.Arrays;

public class ArrayProgram {

    public static void main(String[] args) {
        int[][] array1 = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
        int[][] array2 = { { 4, 5, 6 }, { 7, 8, 9 }, { 0, 1, 2 } };
        int[][] result = ArrayCopier.joinSecondDimension(array1, array2);
        for (int[] array : result) {
            System.out.println(Arrays.toString(array));
        }
    }
}

class ArrayCopier {

    public static int[][] joinSecondDimension(int[][] array1, int[][] array2) {
        int[][] array1and2 = new int[array1.length][];
        for (int index = 0; index < array1.length; index++) {
            array1and2[index] = join(array1[index], array2[index]);
        }
        return array1and2;
    }

    public static int[] join(int[] array1, int[] array2) {
        int[] array1and2 = new int[array1.length + array2.length];
        System.arraycopy(array1, 0, array1and2, 0, array1.length);
        System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
        return array1and2;
    }
}

Prints:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 7, 8, 9]
[1, 2, 3, 0, 1, 2]

EDIT
Implementation for any arguments number (Variable-Length Argument Lists):

import java.util.Arrays;

public class ArrayProgram {

    public static void main(String[] args) {
        int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[][] array2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[][] array3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        test(array1);
        test(array1, array2);
        test(array1, array2, array3);
    }

    private static void test(int[][]... arrays) {
        int[][] result = ArrayCopier.joinSecondDimension(arrays);
        for (int[] array : result) {
            System.out.println(Arrays.toString(array));
        }
        System.out.println();
    }
}

class ArrayCopier {

    public static int[][] joinSecondDimension(int[][]... arrays) {
        int firstArrayLength = arrays[0].length;
        int[][] result = new int[firstArrayLength][];
        for (int index = 0; index < firstArrayLength; index++) {
            result[index] = join(getSecondDimArrays(index, arrays));
        }
        return result;
    }

    public static int[] join(int[]... arrays) {
        int[] result = new int[getTotalLength(arrays)];
        int destPos = 0;
        for (int[] array : arrays) {
            System.arraycopy(array, 0, result, destPos, array.length);
            destPos += array.length;
        }
        return result;
    }

    private static int getTotalLength(int[]... arrays) {
        int length = 0;
        for (int[] array : arrays) {
            length += array.length;
        }
        return length;
    }

    private static int[][] getSecondDimArrays(int index, int[][]... arrays) {
        int[][] result = new int[arrays.length][];
        int resultIndex = 0;
        for (int[][] array : arrays) {
            result[resultIndex++] = array[index];
        }
        return result;
    }
}

Prints:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

[1, 2, 3, 1, 2, 3]
[4, 5, 6, 4, 5, 6]
[7, 8, 9, 7, 8, 9]

[1, 2, 3, 1, 2, 3, 1, 2, 3]
[4, 5, 6, 4, 5, 6, 4, 5, 6]
[7, 8, 9, 7, 8, 9, 7, 8, 9]
Community
  • 1
  • 1
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • This is exactly what I need.... Edit: But why TO string btw??? This line: System.out.println(Arrays.toString(array)); – Marco Berrocal Nov 25 '12 at 22:21
  • If I didn't use "Arrays.toString" method, then application could print output like this: [I@63e68a2b [I@3479404a [I@46bd530. Documentation: "Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]")..." Without it You could see only the reference to an array. If this answer is OK for You, please, mark it as "accepted answer". – Michał Ziober Nov 25 '12 at 22:28
  • Thank you. It worked sans that as well. I will try and figure your way out to print 9 arrays of 3x3 each. The thing is I am doing a Sudoku and need each array isolated as I have to check it's sum per array, see if they match or not. – Marco Berrocal Nov 25 '12 at 22:41
  • I'm glad I can help You. If You want to print it as "Sudoku", You should think about extracting these arrays to external class(probably Sudoku class) and You should to override toString method. In toString method You should use StringBuilder method to creating string representation of Sudoku board. Maybe for board representation You should use one [9, 9] instead of many [3, 3] arrays. Think about it. – Michał Ziober Nov 25 '12 at 22:59
  • But I need to shuffle the values first and in Sudoku you need to solve the individual [3][3] grids with numbers 1-9. How can I shuffle values of an array so they stay confined to numbers valued from 1-9? The way I see it, Sudoku is a giant [9][9] board with 9 [3][3] grids that need to be solved individually. – Marco Berrocal Nov 25 '12 at 23:06
  • Btw, if it's too much to ask, how do I modify your class so I take 3 arrays? In the join method specifically I have a tough time following the logic of what arraycopy does... – Marco Berrocal Nov 25 '12 at 23:09
  • Oi you blew me away with the variable argument length. AMAZING. I like Java more and more... :) – Marco Berrocal Nov 26 '12 at 04:48
  • @MarcoBerrocal A sudoku **cannot** be solved by individually solving the 9 3x3 grids, you also need to ensure that all numbers from 1 through 9 appear on every *row* and *column* as well! Because of this, you usually use a 9x9 array to represent the grid, since this makes accessing rows and columns easy. The 3x3 grids can still be accessed fairly easy with a bit of math on the indices. When using 9 3x3 grids as your base data structure, accessing rows and columns becomes much more complex and a source of headaches. – Mattias Buelens Jun 01 '13 at 14:33
1

Something like this should work perfectly well?

int a1[][] = new int [3][3];
int b1[][] = new int [3][3];
int c1[][] = new int [3][6];

for (int i = 0; i < a1.length; i++) {
    System.arraycopy(a1[i], 0, c1[i], 0, a1[i].length);
    System.arraycopy(a2[i], 0, c1[i], a1[i].length, a2[i].length);
}

And by the way, I assumed from your dimensions that c looks like:

[  a, a, a,  ,  ,  ]
[  a, a, a,  , b,  ]
[  a, a, a,  ,  ,  ]
nair.ashvin
  • 791
  • 3
  • 11