简体   繁体   English

如何从2D java数组中获取列?

[英]How to get a column from a 2D java array?

I know that 2d arrays are arrays of arrays. 我知道2d数组是数组的数组。 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. 由于每行可以是不同的大小,我假设它不是为了从2D数组中获取列而构建的。 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? 这是唯一的方法吗?

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. 祝好运。

Commons math has some tools you might want to check out: Commons math有一些你可能想要查看的工具:

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

Commons Math Library Commons Math Library

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) (或两种方式)

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. 没有更简单的方法。

Actually the newest version of Apache Commons (3.5) doesn't have BigMatrix class. 实际上最新版本的Apache Commons(3.5)没有BigMatrix类。 Instead of this we can use RealMatrix 而不是这个,我们可以使用RealMatrix

double[][] data = new double[10][10];
RealMatrix rm = new Array2DRowRealMatrix(data);
rm.getColumn(i);
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]; 
    }
}

Another way is to store the rows as columns and vice versa. 另一种方法是将行存储为列,反之亦然。 eg I needed to do exactly the same thing and I was originally planning to have an array with 10 rows and 2 cols. 例如,我需要做同样的事情,而我原本计划拥有一个包含10行和2列的数组。 Because of this limitation, I just swapped my rows and columns and created an array with 10 columns and 2 rows. 由于这个限制,我只是交换了我的行和列,并创建了一个包含10列和2行的数组。 Then I can use array[0] to get the row from the new array (which would be a column of my original array). 然后我可以使用array [0]从新数组中获取行(这将是我原始数组的一列)。 Of course you have this flexibility only if you are the creator of that array. 当然,只有当您是该阵列的创建者时,才具有这种灵活性。

Hope that helps... 希望有帮助......

    /**
     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. 在这里,它将以2D矩阵打印一个完整的列数据。

You have to use StringBuilder class to append new character at the end of a string 您必须使用StringBuilder类在字符串的末尾追加新字符

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

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 (ie 6x5 ==> 5x6 2Darray) 转置2D阵列/矩阵(即6x5 ==> 5x6 2Darray)
  • Perform operations on column arrays 对列数组执行操作
  • transpose again ==> get back to your original 2Darray. 再次转置==>回到原来的2D阵列。

This approach have been used in seam carving - image cropping technique 这种方法已被用于接缝雕刻 - 图像裁剪技术

try this 试试这个

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

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM