使用Java从2D数组矩阵中获取行和列

时间:2022-01-19 01:26:33

Suppose that I have a 2D array (matrix) in Java like this...

假设我有一个像Java这样的2D数组(矩阵)......

int[][] MyMat = {{0,1,2,3,4}, {9,8,7,6,5}};

If I want to extract the columns, I can do it easily like this...

如果我想提取列,我可以很容易地这样做...

int[] My0= MyMat[0]; //My0 = {0,1,2,3,4}
int[] My1= MyMat[1]; //My1 = {9,8,7,6,5}

But how can I extract the rows?...

但是如何提取行?...

int[] My_0= ?; //My_0 = {0,9}
int[] My_1= ?; //My_1 = {1,8}
int[] My_2= ?; //My_2 = {2,7}
int[] My_3= ?; //My_3 = {3,6}
int[] My_4= ?; //My_4 = {4,5}

Is there any shorthand for achieving this?

有没有实现这一目标的简写?

3 个解决方案

#1


3  

If you want to get the rows, you need to get the values from each array, then create a new array from the values. You can assign the values manually, or use a for-loop, such as this...

如果要获取行,则需要从每个数组中获取值,然后根据值创建新数组。您可以手动分配值,也可以使用for循环,例如...

int[][] MyMat = {{0,1,2,3,4}, {9,8,7,6,5}};

// get your columns... (easy)
int[] My0= MyMat[0]; //My0 = {0,1,2,3,4}
int[] My1= MyMat[1]; //My1 = {9,8,7,6,5}

// get the rows... (manually)
int[] My_0= new int[]{MyMat[0][0],MyMat[1][0]}; //My_0 = {0,9}
int[] My_1= new int[]{MyMat[0][1],MyMat[1][1]}; //My_1 = {1,8}
int[] My_2= new int[]{MyMat[0][2],MyMat[1][2]}; //My_2 = {2,7}
int[] My_3= new int[]{MyMat[0][3],MyMat[1][3]}; //My_3 = {3,6}
int[] My_4= new int[]{MyMat[0][4],MyMat[1][4]}; //My_4 = {4,5}

// get the rows... (as a for-loop)
int size = MyMat.length;
int[] My_0 = new int[size]; //My_0 = {0,9}
int[] My_1 = new int[size]; //My_1 = {1,8}
int[] My_2 = new int[size]; //My_2 = {2,7}
int[] My_3 = new int[size]; //My_3 = {3,6}
int[] My_4 = new int[size]; //My_4 = {4,5}
for (int i=0;i<size;i++){
    My_0[i] = MyMat[i][0];
    My_1[i] = MyMat[i][1];
    My_2[i] = MyMat[i][2];
    My_3[i] = MyMat[i][3];
    My_4[i] = MyMat[i][4];
}

Otherwise, turn your entire array around so that it stores {row,column} instead of {column,row}, like this...

否则,转动整个数组,使其存储{row,column}而不是{column,row},就像这样......

int[][] MyMat = {{0,9},{1,8},{2,7},{3,6},{4,5}};

// get the rows... (easy)
int[] My_0= MyMat[0]; //My_0 = {0,9}
int[] My_1= MyMat[1]; //My_1 = {1,8}
int[] My_2= MyMat[2]; //My_2 = {2,7}
int[] My_3= MyMat[3]; //My_3 = {3,6}
int[] My_4= MyMat[4]; //My_4 = {4,5}

// get the columns... (manually)
int[] My0= new int[]{MyMat[0][0],MyMat[1][0],MyMat[2][0],MyMat[3][0],MyMat[4][0]}; //My0 = {0,1,2,3,4}
int[] My1= new int[]{MyMat[0][1],MyMat[1][1],MyMat[2][1],MyMat[3][1],MyMat[4][1]}; //My1 = {9,8,7,6,5}

// get the columns... (as a for-loop)
int size = MyMat.length;
int[] My0 = new int[size]; //My0 = {0,1,2,3,4}
int[] My1 = new int[size]; //My1 = {9,8,7,6,5}
for (int i=0;i<size;i++){
    My0[i] = MyMat[0][i];
    My1[i] = MyMat[1][i];
}

Note that it isn't possible to have a shorthand that will allow you to get both the rows and the columns easily - you'll have to decide which you want more, and structure the arrays to be in that format.

请注意,不可能有一个简单的方法,可以让你轻松获得行和列 - 你必须决定你想要更多,并将数组结构化为该格式。

#2


1  

If we know the size row and column size of 2-d array we can achieve above as follows

如果我们知道二维数组的大小行和列大小,我们可以通过以下方式实现

Let No.of Rows - rows

让行数 - 行

Let No.of Columns -clmns

列号为-clmns

int[][] my = new int[clmns][rows];
for(int i=0;i<clmns;i++)
for(int j=0;j< rows; j++)
 my[i][j]=MyMat[j][i]; 

Then taking a column at time give you the row array of your original array.

然后在时间上获取一列,为您提供原始数组的行数组。

Otherwise you can use Array of ArrayList with Array length of your rows if the no.of rows were given at run time of the program.

否则,如果在程序的运行时给出了no.of行,则可以使用Array of ArrayList和行的Array长度。

#3


1  

Its simple as this :

它很简单:

1 . Transpose your 2D matrix 2 . Then do as you meint[] My0= MyMat[0]; int[] My1= MyMat[1];

1。转置您的2D矩阵2。然后你做meint [] My0 = MyMat [0]; int [] My1 = MyMat [1];

#1


3  

If you want to get the rows, you need to get the values from each array, then create a new array from the values. You can assign the values manually, or use a for-loop, such as this...

如果要获取行,则需要从每个数组中获取值,然后根据值创建新数组。您可以手动分配值,也可以使用for循环,例如...

int[][] MyMat = {{0,1,2,3,4}, {9,8,7,6,5}};

// get your columns... (easy)
int[] My0= MyMat[0]; //My0 = {0,1,2,3,4}
int[] My1= MyMat[1]; //My1 = {9,8,7,6,5}

// get the rows... (manually)
int[] My_0= new int[]{MyMat[0][0],MyMat[1][0]}; //My_0 = {0,9}
int[] My_1= new int[]{MyMat[0][1],MyMat[1][1]}; //My_1 = {1,8}
int[] My_2= new int[]{MyMat[0][2],MyMat[1][2]}; //My_2 = {2,7}
int[] My_3= new int[]{MyMat[0][3],MyMat[1][3]}; //My_3 = {3,6}
int[] My_4= new int[]{MyMat[0][4],MyMat[1][4]}; //My_4 = {4,5}

// get the rows... (as a for-loop)
int size = MyMat.length;
int[] My_0 = new int[size]; //My_0 = {0,9}
int[] My_1 = new int[size]; //My_1 = {1,8}
int[] My_2 = new int[size]; //My_2 = {2,7}
int[] My_3 = new int[size]; //My_3 = {3,6}
int[] My_4 = new int[size]; //My_4 = {4,5}
for (int i=0;i<size;i++){
    My_0[i] = MyMat[i][0];
    My_1[i] = MyMat[i][1];
    My_2[i] = MyMat[i][2];
    My_3[i] = MyMat[i][3];
    My_4[i] = MyMat[i][4];
}

Otherwise, turn your entire array around so that it stores {row,column} instead of {column,row}, like this...

否则,转动整个数组,使其存储{row,column}而不是{column,row},就像这样......

int[][] MyMat = {{0,9},{1,8},{2,7},{3,6},{4,5}};

// get the rows... (easy)
int[] My_0= MyMat[0]; //My_0 = {0,9}
int[] My_1= MyMat[1]; //My_1 = {1,8}
int[] My_2= MyMat[2]; //My_2 = {2,7}
int[] My_3= MyMat[3]; //My_3 = {3,6}
int[] My_4= MyMat[4]; //My_4 = {4,5}

// get the columns... (manually)
int[] My0= new int[]{MyMat[0][0],MyMat[1][0],MyMat[2][0],MyMat[3][0],MyMat[4][0]}; //My0 = {0,1,2,3,4}
int[] My1= new int[]{MyMat[0][1],MyMat[1][1],MyMat[2][1],MyMat[3][1],MyMat[4][1]}; //My1 = {9,8,7,6,5}

// get the columns... (as a for-loop)
int size = MyMat.length;
int[] My0 = new int[size]; //My0 = {0,1,2,3,4}
int[] My1 = new int[size]; //My1 = {9,8,7,6,5}
for (int i=0;i<size;i++){
    My0[i] = MyMat[0][i];
    My1[i] = MyMat[1][i];
}

Note that it isn't possible to have a shorthand that will allow you to get both the rows and the columns easily - you'll have to decide which you want more, and structure the arrays to be in that format.

请注意,不可能有一个简单的方法,可以让你轻松获得行和列 - 你必须决定你想要更多,并将数组结构化为该格式。

#2


1  

If we know the size row and column size of 2-d array we can achieve above as follows

如果我们知道二维数组的大小行和列大小,我们可以通过以下方式实现

Let No.of Rows - rows

让行数 - 行

Let No.of Columns -clmns

列号为-clmns

int[][] my = new int[clmns][rows];
for(int i=0;i<clmns;i++)
for(int j=0;j< rows; j++)
 my[i][j]=MyMat[j][i]; 

Then taking a column at time give you the row array of your original array.

然后在时间上获取一列,为您提供原始数组的行数组。

Otherwise you can use Array of ArrayList with Array length of your rows if the no.of rows were given at run time of the program.

否则,如果在程序的运行时给出了no.of行,则可以使用Array of ArrayList和行的Array长度。

#3


1  

Its simple as this :

它很简单:

1 . Transpose your 2D matrix 2 . Then do as you meint[] My0= MyMat[0]; int[] My1= MyMat[1];

1。转置您的2D矩阵2。然后你做meint [] My0 = MyMat [0]; int [] My1 = MyMat [1];