6

I have a situation where I need to concatenate two two-dimensional arrays.

Object[][] getMergedResults() {
    Object[][] a1 = getDataFromSource1();
    Object[][] a2 = getDataFromSource2();
    // I can guarantee that the second dimension of a1 and a2 are the same
    // as I have some control over the two getDataFromSourceX() methods

    // concat the two arrays
    List<Object[]> result = new ArrayList<Object[]>();
    for(Object[] entry: a1) {
        result.add(entry);
    }
    for(Object[] entry: a2) {
        result.add(entry);
    }
    Object[][] resultType = {};

    return result.toArray(resultType);
}

I have looked at the solutions for the concatenation of 1-dimensional arrays in this post but have been unable to make it work for my two-dimensional arrays.

So far, the solution I have come up with is to iterate over both arrays and adding each member to a ArrayList and then returning toArray() of that array list. I'm sure there must be an easier solution, but have so far been unable to come with one.

Community
  • 1
  • 1
Urs Beeli
  • 746
  • 1
  • 13
  • 30

4 Answers4

8

You could try

Object[][] result = new Object[a1.length + a2.length][];

System.arraycopy(a1, 0, result, 0, a1.length);
System.arraycopy(a2, 0, result, a1.length, a2.length);
  • Thank you. Strangely enough, this looks exactly like the one-dimensional solution proposed in the link I mentioned above. I had tried to adapt that to my two-dimensional issue but couldn't make it work. Now, looking at your solution I don't see what I'd done differently for it not to work. *scratches head*. Anyway, I'm glad I've got a working solution. – Urs Beeli Dec 05 '12 at 13:01
1

You could use Apache Commons Library - ArrayUtils. Change only the index for second dimension and merge the whole lines.

Andrejs
  • 10,803
  • 4
  • 43
  • 48
user219882
  • 15,274
  • 23
  • 93
  • 138
  • Thanks, that looks like an interesting library, though I don't think I'll want to added dependency if the above code solves my problem. Still, I'll keep it in mind for future reference. – Urs Beeli Dec 05 '12 at 13:27
0

Here is the method I use for 2D array concatenation. It partly uses Sergio Nakanishi's answer, but adds the ability to concatenate in both directions.

/*
 * Define directions for array concatenation
 */
public static final byte ARRAY_CONCAT_HORIZ = 0, ARRAY_CONCAT_VERT = 1;

/*
 * Concatenates 2 2D arrays
 */
public static Object[][] arrayConcat(Object[][] a, Object[][] b, byte concatDirection)
{
    if(concatDirection == ARRAY_CONCAT_HORIZ && a[0].length == b[0].length)
    {
        return Arrays.stream(arrayConcat(a, b)).map(Object[].class::cast)
                    .toArray(Object[][]::new);
    }
    else if(concatDirection == ARRAY_CONCAT_VERT && a.length == b.length)
    {
        Object[][] arr = new Object[a.length][a[0].length + b[0].length];
        for(int i=0; i<a.length; i++)
        {
            arr[i] = arrayConcat(a[i], b[i]);
        }
        return arr;
    }
    else
        throw new RuntimeException("Attempted to concatenate arrays of incompatible sizes.");
}

/*
 * Concatenates 2 1D arrays
 */
public static Object[] arrayConcat(Object[] a, Object[] b)
{
    Object[] arr = new Object[a.length + b.length];
    System.arraycopy(a, 0, arr, 0, a.length);
    System.arraycopy(b, 0, arr, a.length, b.length);
    return arr;
}
nullromo
  • 2,165
  • 2
  • 18
  • 39
0

Logical Way-

    for(int j = 0; j < a.length+b.length; j++){
        if(j < a.length){
            ans[j] = a[j];
        }else{
            ans[j] = b[j-(a.length)];
        }
    }
Anand Jha
  • 17
  • 3