I know that 2d arrays are arrays of arrays. To get a row you can do:
我知道2d数组是数组的数组。要获得一行,你可以做:
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:
由于每行可以是不同的大小,我假设它不是为了从2D数组中获取列而内置的。这让我相信你必须做的事情如下:
for(int row = 0; row < numRows; row++)
{
colArray[row] = m2Darray[row][columnOfInterest];
}
Is this correct? Is it the only way?
它是否正确?这是唯一的方法吗?
11 个解决方案
#1
8
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
7
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
#3
2
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
2
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];
}
}
#5
1
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.
顺便说一句:我想你是对的。没有更简单的方法。
#6
1
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);
#7
0
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...
希望有帮助......
#8
0
/**
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矩阵打印一个完整的列数据。
#9
-1
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]);
#10
-2
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)
- 一个。转置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
这种方法已被用于接缝雕刻 - 图像裁剪技术
#11
-4
try this
尝试这个
int column = 3;
double result = array[][column];
Good Luck
祝你好运
#1
8
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
7
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
#3
2
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
2
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];
}
}
#5
1
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.
顺便说一句:我想你是对的。没有更简单的方法。
#6
1
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);
#7
0
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...
希望有帮助......
#8
0
/**
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矩阵打印一个完整的列数据。
#9
-1
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]);
#10
-2
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)
- 一个。转置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
这种方法已被用于接缝雕刻 - 图像裁剪技术
#11
-4
try this
尝试这个
int column = 3;
double result = array[][column];
Good Luck
祝你好运