How to get a column from a 2D java array?

2020-02-21 07:24发布

问题:

I know that 2d arrays are arrays of arrays. To get a row you can do:

rowArray = my2Darray[row]

Since each row can be a different size, I'm assuming it's not built in to get a column from a 2D array. It leads me to believe you'd have to do something like:

for(int row = 0; row < numRows; row++)
{
    colArray[row] = m2Darray[row][columnOfInterest];
}

Is this correct? Is it the only way?

回答1:

If you are locked down to using a 2d array, then yes, this is it afaik. However, a suggestion that may help you (if possible):

Wrap the array in a class that handles the column fetching.

Good luck.



回答2:

Commons math has some tools you might want to check out:

double[][] data = new double[10][10];
BigMatrix matrix = MatrixUtils.createBigMatrix(data);
matrix.getColumnAsDoubleArray(0);

Commons Math Library



回答3:

Your way is the way to go. However, if you have to do that many times, I may recommended storing it in columns. (or both ways)



回答4:

Well actually I'd write this as a comment, but my reputation is still to low, so I have to answer:

Guess you mean:

for(int row = 0; row < numRows; row++)
{
    colArray[row] = m2Darray[row][columnOfInterest];
}

BTW: I suppose you are right. There is no easier way.



回答5:

int[][] array = new int[rows][coloumn];

for (int i = 0 ; i < array.length ; i++) {
    for (int j = 0 ; j < array[].length; j++) {
        int col = array[j][i]; 
    }
}


回答6:

Actually the newest version of Apache Commons (3.5) doesn't have BigMatrix class. Instead of this we can use RealMatrix

double[][] data = new double[10][10];
RealMatrix rm = new Array2DRowRealMatrix(data);
rm.getColumn(i);


回答7:

    /**
     input data
    **/
    int[][] r = {
            {1, 2},
            {3, 4}
    };
    //the colum index
    int colIndex = 1;
    Integer[] col = Arrays.stream(r).stream().map(arr -> arr[colIndex]).toArray(size -> new Integer[size]);
    //col data
    for (Integer integer : col) {
        System.out.println(integer);
    }

Here it will print one whole column data in 2D matrix.



回答8:

Another way is to store the rows as columns and vice versa. e.g. I needed to do exactly the same thing and I was originally planning to have an array with 10 rows and 2 cols. Because of this limitation, I just swapped my rows and columns and created an array with 10 columns and 2 rows. Then I can use array[0] to get the row from the new array (which would be a column of my original array). Of course you have this flexibility only if you are the creator of that array.

Hope that helps...



回答9:

You have to use StringBuilder class to append new character at the end of a string

    StringBuilder s=new StringBuilder();
    for(int i=0;i<n;i++)
        s.append(arr[i][column]);


回答10:

Just came across this post by chance. Another way to perform operations such as array copying or manipulation on column arrays is to transpose your array/matrix.

Shortly speaking

  • a. transpose 2Darray / matrix (i.e. 6x5 ==> 5x6 2Darray)
  • Perform operations on column arrays
  • transpose again ==> get back to your original 2Darray.

This approach have been used in seam carving - image cropping technique



回答11:

try this

int column = 3;
double result = array[][column];

Good Luck