0

There is a really easy way to access the rows of a 2D array in java

for (int i = 0 ; i < integer2D.length ; i++)
    getMyArray(integer2D[i]);

But, I searched in the web to find such easy way to iterate on columns of the 2D-array, like

for (int j = 0 ; j < integer2D[0].length ; j++)
    getMyArray(integer2D[][i]);

or

for (int j = 0 ; j < integer2D[0].length ; j++)
    getMyArray(integer2D[...][i]);

which works in some programming languages. I just found the class RealMatrix and MatrixUtils that I can convert my array2D to a real matrix and then transpose it and again convert it to an array and iterate on it. But I suppose that there exist a simpler way?

Edit: iterating on rows as I noted in the first piece of code is easy but the main question is how to iterate on columns like the second and third codes work in some other programming languages.

Edit2: As I mentioned in the last paragraph of the main question, the easiest way that I know is transposing the matrix and the iterating on its rows.

2
  • what is the difference between your array2d and real matrix? Commented Nov 19, 2014 at 9:17
  • What do you want to pass to getMyArray ? An array that represent the whole j-th column ? In which case no, I don't think there is an easy way to do that... (it might help to use a single array, put every items in sequence, and use ((i * lines_length) + j) as the index of an element...) Commented Nov 19, 2014 at 9:21

3 Answers 3

2

If I understand your question, you could use a for-each as an easy way to get each row like

for (int[] row : integer2D) { // <-- for each int[] in the int[][]
  for (int val : row) { // <-- for each int in the int[] row
    // ...
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The question is iterating on columns. In some programming languages matrix[][i] gives the iTH column of the matrix.
@hossayni Java isn't one of them. Each array is an independent object.
0

For this, you can use the BigMatrixImpl Class of the commons math library. It has a getColumnAsDoubleArray() method which will return the specified column as an array.

Comments

0

Delivering only one index will give you the whole row, so:

integer2D[5] // returns an int[] 

will give you an integer array, which is the 6th row in your matrix.

If you supply both indexes directly you get the value of the "cell"

integer2D[5][1] // returns an int

will give you the value of the second column of the 6th row.

This is direct access to your matrix, if you want to iterate through the rows the answer from Elliott is what you are looking for.

Edit: Transposing:

int width = array.length;
int height = array[0].length; 

int[][] array_new = new int[height][width];

for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
    array_new[y][x] = array[x][y];
  }
}

2 Comments

As I mentioned by the second and third piece of codes I would like to iterate on columns like integer2D[][5] which gives the 6 column of matrix in some programming languages.
It is not possible in java like this, you have to transpose it and the get the rows of the transposed array. See edit.

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.