dcrearer
dcrearer

Reputation: 2062

Column major order

If I have a two dimensional array and don't know the size. How can I process it in column major order without throwing an exception? My printColRow function below fails because of the index being out of bounds.

private void printRowCol(int[][] a) {
    for(int i = 0; i < a.length; i++) {
        double sum = 0.0;
        for(int j = 0; j < a[i].length; j++)  
                sum += a[i][j];
        System.out.println(sum + " ");
        }
}
private void printColRow(int[][] a){
    for(int i = 0; i < a[i].length; i++) {
        double sum = 0.0;
        for(int j = 0; j < a.length; j++)
                sum += a[j][i];
        System.out.println(sum + " ");
        }
}

Upvotes: 0

Views: 6190

Answers (2)

kwarnke
kwarnke

Reputation: 1514

In opposite to row major order, where you handle all values of a row before switching to the next row, at column major order all values of a column are handled before switching to the next column . See Row-major_order for a full explanation. If you have:

final int[][] array = {
    {11, 12, 13},
    {21, 22, 23}};

final int n_row = array.length;
final int n_col = array[0].length;

then this prints:

for (int i_col = 0; i_col < n_col; i_col++) {
    System.out.printf("col[%d]:", i_col);
    for (int i_row = 0; i_row < n_row; i_row++) {
        System.out.printf(" %d", array[i_row][i_col]);
    }
    System.out.println();
}

the values in column major order. There are three columns with two values each:

col[0]:11 21
col[1]:12 22
col[2]:13 23

This works only for a rectangular array. That is, every row contains the same count of values. In opposite this prints:

for (int i_row = 0; i_row < n_row; i_row++) {
    System.out.printf("row[%d]:", i_row);
    for (int i_col = 0; i_col < n_col; i_col++) {
        System.out.printf(" %d", array[i_row][i_col]);
    }
    System.out.println();
}

the values in the for java usual row major order:

row[0]: 11 12 13
row[1]: 21 22 23

Two rows with three values each. Due to the fact that a two dimensional array is a one dimensional array (of rows) which contains each one dimensional array as a value (a column) you can do that shorter:

for (final int[] row: array) {
    System.out.print("row:");
    for (final int val: row) {
        System.out.printf(" %d", val);
    }
    System.out.println();
}

The outer loop iterates over a int[][], where each row contains a int[] as value. In inner loop iterates then over the value int[] array.

The Java 8 way is not so elegant due that the array is an array of primitives int[]:

Arrays.asList(array).forEach(row -> { for (final int val: row) System.out.println(val); });

Upvotes: 0

Paul Boddington
Paul Boddington

Reputation: 37645

If the array has at least one row (a.length != 0) and all the rows have the same length, then you can do it like this:

private void printColRow(int[][] a){
    for(int i = 0; i < a[0].length; i++) {
        double sum = 0.0;
        for(int j = 0; j < a.length; j++)
            sum += a[j][i];
        System.out.println(sum + " ");
    }
}

If the rows can have different lengths (e.g. something like {{1, 2, 3}, {4}, {5, 6}}), then something more complex would be required.

Upvotes: 1

Related Questions