3

I am trying to implement Strassen's Algorithm in Java and I am at a step where I need to combine the output into a single matrix/2D array. I am using System.arraycopy to copy the arrays, which works well for concatenating two arrays in a top-down manner, however, I also need to concatenate them side-by-side and I am having trouble with that. I run into ArrayOutOfBoundsException. Here is my code

static int[][] Consolidate(int[][] c11, int[][] c12, int[][] c21, int[][] c22) {
    /* check size compatibility */
    if(c11[0].length == c21[0].length && 
            c11.length == c12.length &&
            c21.length == c22.length &&
            c22[0].length == c12[0].length) {
        int _rowSize = c11.length + c21.length;
        int _colSize = c11[0].length + c12[0].length;
        int[][] retArray = new int[_rowSize][_colSize];

        int[][] ltArray = new int[_rowSize][c11[0].length];
        int[][] rtArray = new int[_rowSize][c12[0].length];

        System.arraycopy(c11, 0, ltArray, 0, c11.length);
        System.arraycopy(c21, 0, ltArray, c11.length, c21.length);
        System.arraycopy(c12, 0, rtArray, 0, c12.length);
        System.arraycopy(c22, 0, rtArray, c12.length, c22.length);

        System.arraycopy(ltArray, 0, retArray, 0, ltArray.length);
        System.arraycopy(rtArray, 0, retArray, ltArray.length, rtArray.length);
        return retArray;
    }
    return null;
}

The last line

System.arraycopy(rtArray, 0, retArray, ltArray.length, rtArray.length);

Throws the exception. Is there a way to concatenate Arrays side-by-side (in the column-wise manner)?

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Vinay Pandey
  • 1,129
  • 3
  • 16
  • 33

2 Answers2

1

EDIT Here is the verified answer

You can copy the last part manually.

Replace

System.arraycopy(ltArray, 0, retArray, 0, ltArray.length);
System.arraycopy(rtArray, 0, retArray, ltArray.length, rtArray.length);

With

//Commented both calls
//System.arraycopy(ltArray, 0, retArray, 0, ltArray.length);
//System.arraycopy(rtArray, 0, retArray, ltArray.length, rtArray.length);
for (int row = 0; row < ltArray.length; row++) {
    int colInTarget = 0;
    for (int col = 0; col < ltArray[row].length; col++,colInTarget++) {
        retArray[row][colInTarget] = ltArray[row][col];
    }
    for (int col = 0; col < rtArray[row].length; col++,colInTarget++) {
        retArray[row][colInTarget] = rtArray[row][col];
    }
}
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • The sizes are correctly computed as is seen in `int[][] retArray = new int[_rowSize][_colSize]`. I think the problem is that Java is trying to append the array at the bottom (row-wise manner) of the `retArray` instead of column-wise manner. I was wondering if there is a way to tell java to append in column-wise manner – Vinay Pandey Sep 02 '12 at 15:15
  • If there is no other way, then I guess I will have to put in loops, which I was trying to avoid. Thanks for your help! – Vinay Pandey Sep 02 '12 at 16:09
1

I suppose there are no way to concatenate column by column, as you could do it in octave or matlab.

The reason behind this is java support one-dimensional arrays. And matrix is one-dimensional array where each element is an array also (not mandatory to be the same size!).

So, the most straightforward solution to copy element-by-element in a loop.

private static int[][] consolidate(int[][] a, int[][] b, int[][] c, int[][] d) {
        int n = a.length * 2;
        int[][] result = new int[n][n];
        copy(result, a, 0, 0);
        copy(result, b, 0, n / 2);
        copy(result, c, n / 2, 0);
        copy(result, d, n / 2, n / 2);
        return result;
    }

    private static void copy(int[][] result, int[][] a, int m, int n) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                result[m + i][n + j] = a[i][j];
            }
        }
    }
mishadoff
  • 10,719
  • 2
  • 33
  • 55