Suppose that I have a 2D array (matrix) in Java like this…
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?
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…
Otherwise, turn your entire array around so that it stores
{row,column}instead of{column,row}, like this…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.