从java中的二维数组中获取列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30426909/
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
get columns from two dimensional array in java
提问by Pedro Gonzalez
In a two dimensional array I can easily get the arrays for the rows, how can I get the columns as arrays too? I need a solution that works for objects too, not just primitives. Thanks
在二维数组中,我可以轻松获取行的数组,如何将列也获取为数组?我需要一个也适用于对象的解决方案,而不仅仅是基元。谢谢
int counter = 1;
int[][] matrix = new int[9][9];
for (int x = 0; x < matrix.length; x++) {
for (int y = 0; y < matrix[0].length; y++) {
matrix[x][y] = counter;
System.out.print(counter + " ");
counter++;
}
System.out.println();
}
for (int x = 0; x < matrix.length; x++) {
int[] row = matrix[x];
}
采纳答案by Eric Leibenguth
There's no "out-of-the-box" way, but you can create a static method for this:
没有“开箱即用”的方式,但您可以为此创建一个静态方法:
public static Object[] getColumn(Object[][] array, int index){
Object[] column = new Object[array[0].length]; // Here I assume a rectangular 2D array!
for(int i=0; i<column.length; i++){
column[i] = array[i][index];
}
return column;
}
回答by Eran
There's no easy way. You have to iterate over the array and construct the column arrays yourself.
没有简单的方法。您必须遍历数组并自己构建列数组。
You should note that the "column arrays" are not well defined if your 2D array has rows of different lengths. For example, how would you create the 3rd column array if the 2nd row has only 2 columns?
您应该注意,如果您的 2D 数组具有不同长度的行,则“列数组”没有很好地定义。例如,如果第 2 行只有 2 列,您将如何创建第 3 列数组?
If your 2D array is a matrix (i.e. all the rows have the same length), it's easy enough to create a 2D array whose rows are the columns of the original 2D array. Then you can get the columns of the original arrays easily.
如果您的二维数组是一个矩阵(即所有行都具有相同的长度),那么创建一个二维数组很容易,其行是原始二维数组的列。然后您可以轻松获取原始数组的列。
回答by JonasCz - Reinstate Monica
You can do it like this:
你可以这样做:
for(int row = 0; row < numRows; row++) {
colArray[row] = my2Darray[row][columnOfInterest];
}
Apache commons has tools for this, check this answer.
Apache commons 有这方面的工具,检查这个答案。
回答by sprinter
Here is a method using Java 8 streams:
这是一个使用 Java 8 流的方法:
int[] getColumn(int[][] matrix, int column) {
return IntStream.range(0, matrix.length)
.map(i -> matrix[i][column]).toArray();
}
And if you want to cope with rows that are too short:
如果你想处理太短的行:
int[] getColumn(int[][] matrix, int column, int defaultVal) {
return IntStream.range(0, matrix.length)
.map(i -> matrix[i].length < column ? defaultVal : matrix[i][column])
.toArray();
}
回答by Buzzaard
Here is another Java 8 answer that works for objects:
这是另一个适用于对象的 Java 8 答案:
// Make 2D array of objects
Obj[][] obj = {{new Obj(0),new Obj(1)},{new Obj(2),new Obj(3)}};
// To get a row:
int rowToGet = 1;
Obj[] oRow = obj[rowToGet];
// To get a column:
int colToGet = 1;
Obj[] oCol = Arrays.stream(obj).map(o -> o[colToGet]).toArray(Obj[]::new);
// Print the row:
System.out.println(Arrays.toString(oRow));
// Print the column:
System.out.println(Arrays.toString(oCol));
// Here is the object used for this example
class Obj {
int in;
String str;
public Obj(){};
public Obj(int in){
this.in = in;
switch(in){
case 0:
str = "zero";
break;
case 1:
str = "one";
break;
case 2:
str = "two";
break;
case 3:
str = "three";
break;
}
}
@Override
public String toString(){
return String.format("%d=%s",in,str);
}
}
This will produce the output:
这将产生输出:
Extracted row: [2=two, 3=three]
Extracted col: [1=one, 3=three]
Extracted row: [2=two, 3=three]
Extracted col: [1=one, 3=three]
回答by Geoff Hayward
You can do it this way:
你可以这样做:
private <T> T[] getColumn(int address, T[][] from) {
return (T[]) Arrays.stream(from).map(x -> x[address]).toArray(Object[]::new);
}