4

I am looking for a way to traverse a 2d n by m int array (int[col][row]) first row by row (simple part) and then column by column, in Java. Here is the code for doing row by row, is there a way to do col by col?

for(int i = 0; i < display.length; i++){
            for (int j = 0; j < display[i].length; j++){
                if (display[i][j] == 1)
                    display[i][j] = w++;
                else w = 0;
            }
        }
4
  • Variable width columns? Commented Jan 29, 2014 at 18:35
  • 1
    What if you change display[i][j] to display[j][i] inside the loops? Commented Jan 29, 2014 at 18:38
  • not variable width collumns. If you changed it since it is n by m grid it would change the orientation which wouldn't be desirable Commented Jan 29, 2014 at 18:44
  • If one of the answers below worked for you, please accept it. Commented Jan 29, 2014 at 23:38

5 Answers 5

5

Since you used a two dimensional array as a matrix, we can assume that the length of each row is the same throughout the matrix (i.e. number of columns of each row is the same).

//So, you can treat display[0].length as the number of columns.

for(int col=0; col<display[0].length; col++)
{
   for(int row=0; row<display.length; row++)
   {
      //your code to access display[row][col]
   }
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

This seems to make sense. Thanks!
Yeah, as hexafraction mentioned, Java doesn't treat multidimensional arrays as rectangular. The above code can only be used if you can safely assume that your array is a matrix.
4

Here is one approach that will print by column if the row has that many columns column.

String[][] twoDArray = new String[][] {
        new String[] {"Row1Col1", "Row1Col2", "Row1Col3"},
        new String[] {"Row2Col1", "Row2Col2"},
        new String[] {"Row3Col1", "Row3Col2", "Row3Col3", "Row3Col4"}
};

boolean recordFound = true;
int colIndex = 0;
while(recordFound) {
    recordFound = false;
    for(int row=0; row<twoDArray.length; row++) {
        String[] rowArray = twoDArray[row];
        if(colIndex < rowArray.length) {
            System.out.println(rowArray[colIndex]);
            recordFound = true;
        }
    }
    colIndex++;
}

Output is:

Row1Col1
Row2Col1
Row3Col1
Row1Col2
Row2Col2
Row3Col2
Row1Col3
Row3Col3
Row3Col4

Comments

1

It's not a very "natural" thing to happen, due to the way Java arrays are nested and not rectangular multidimensional. For example, the following is possible:

[ ][ ][ ]
[ ][ ]
[ ][ ][ ][ ][ ]

where [ ] is an element.

Traversing this vertically is not a very "natural" or efficient operation. You could, however, do so by traversing columns up to the maximum of the array lengths, avoiding array out of bound issues with explicit checks or (being more evil) silently dropping ArrayOutOfBounds exceptions.

Edit: In the rectangular case just switch the two loops. It will not matter which row's length you use.

2 Comments

I see what you are saying, but in this specific case I'm dealing with a rectangular 2d array :)
Alex. If it's a known length then just reverse your for loops. You can always do it based on the length of the first column. See garyF's answer. My answer handles the case hex is referring to.
0
static void columnFirst(List<List<Integer>> matrix) {
    int max = 0;
    for (int i = 0; i < matrix.size(); i++) {
        max = Math.max(max, matrix.get(i).size());
    }


    for (int i = 0; i < max; i++) {
        for (int j = 0; j < matrix.size(); j++) {
            if (matrix.get(j).size() > i)
                System.out.print(matrix.get(j).get(i) + " ");
        }
        System.out.println();
    }
}

Input array

{{1, 2, 3, 4, 5, 6},
 {1, 2, 3},
 {1, 2, 3, 4, 5},
 {1},
 {1, 2, 3, 4, 5, 6, 7, 8, 9}}

Output:

1 1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 
5 5 5 
6 6 
7 
8 
9 

Comments

0
/* 
Assume that the length of the col[0] will be the same for all cols. 
Notice: that we are access salaries[j] for each iteration of j while
[i] same the same. 
*/

public void printColsValues() {
    for(int i = 0; i < array[0].length; i++) {
        for(int j = 0; j < array.length; j ++) {
            System.out.println(arr[j][i]);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.