如何从二维 java 数组中获取一列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1111998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a column from a 2D java array?
提问by Derrick
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?
这样对吗?这是唯一的方法吗?
采纳答案by javamonkey79
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):
如果您被锁定为使用二维数组,那么是的,这就是它afaik。但是,一个可能对您有帮助的建议(如果可能):
Wrap the array in a class that handles the column fetching.
将数组包装在处理列提取的类中。
Good luck.
祝你好运。
回答by jjnguy
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)
你的路就是要走的路。但是,如果您必须多次这样做,我可能建议将其存储在列中。(或两种方式)
回答by daveb
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);
回答by Curd
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.
BTW:我想你是对的。没有更简单的方法。
回答by javamonkey79
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.
另一种方法是将行存储为列,反之亦然。例如,我需要做完全相同的事情,我最初计划有一个包含 10 行和 2 列的数组。由于这个限制,我只是交换了我的行和列,并创建了一个包含 10 列和 2 行的数组。然后我可以使用 array[0] 从新数组中获取行(这将是我原始数组的一列)。当然,只有当您是该数组的创建者时,您才具有这种灵活性。
Hope that helps...
希望有帮助...
回答by Muhammad Daniyal
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];
}
}
回答by David
try this
尝试这个
int column = 3;
double result = array[][column];
Good Luck
祝你好运
回答by bolec_kolec
Actually the newest version of Apache Commons (3.5) doesn't have BigMatrix class. Instead of this we can use RealMatrix
实际上最新版本的 Apache Commons (3.5) 没有 BigMatrix 类。我们可以使用 RealMatrix 代替这个
double[][] data = new double[10][10];
RealMatrix rm = new Array2DRowRealMatrix(data);
rm.getColumn(i);
回答by Paul Mai
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.
- 一种。转置 2Darray / 矩阵(即 6x5 ==> 5x6 2Darray)
- 对列数组执行操作
- 再次转置 ==> 回到原来的 2Darray。
This approach have been used in seam carving - image cropping technique
这种方法已用于接缝雕刻-图像裁剪技术
回答by Harsh Gupta
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]);