You almost have it ready ... only you do not exchange properly with a "temporary" variable and subtract the pointer i
so you can invert the data the way you expect.
for (int i = 0; i < vector2d1.length /2; i++) {
for (int c = 0; c < vector2d1.length ; c++) {
int temp = vector2d1[i][c];
vector2d1[i][c] = vector2d1[vector2d1.length - 1 - i][c];
vector2d1[vector2d1.length - 1 - i][c] = temp;
}
}
The great aggravating factor of this code is that it will fail when the structure of the array is not of equal dimensions, that is, when it is not equal in rows and columns.
verctor2d1.length
as it will return the size in rows, only that, will not give you information about the width of the array 2d, therefore, when it is an array of identical dimensions it will not matter because rows and columns are the same, all ok, but if we have for example a vector2d1[3][5]
the second cycle for
the person in charge of traversing columns will only get up to 3, not up to 5, you would ignore 2 columns of data !; This behavior is possible to correct for example with vector2d1[1].length
, which gives the width.
Going back to your code and now modifying that second cycle for
:
for (int i = 0; i < vector2d1.length /2; i++) {
for (int c = 0; c < vector2d1[1].length; c++) {
int temp = vector2d1[i][c];
vector2d1[i][c] = vector2d1[vector2d1.length - 1 - i][c];
vector2d1[vector2d1.length - 1 - i][c] = temp;
}
}
The program is more generic by inverting the rows of data the way you plan.