Reputation: 159
I have a 2D array that I would like to flip clockwise (no, this is not a homework assignment!). I have the following output as my code tries to flip the array by creating a new temporary one.
// Original array
I/System.out: 10 11 12 13 14
I/System.out: 15 16 17 18 19
// Temp array
I/System.out: 10 15
I/System.out: 11 16
I/System.out: 12 17
I/System.out: 13 18
I/System.out: 14 19
What I want is for the second array (15 - 19) to be in the first column. I just do not understand what I am doing wrong.
The code is as follows:
Object[][][] containerTemp = new Object[zDim][yDim][xDim];
for (int z = 0; z < zDim; z++) {
for (int y = yDim - 1; y >= 0; y--) {
for (int x = 0; x < xDim; x++) {
containerTemp[z][y][x] = container[z][x][y];
}
}
}
The final result of the Temp array will be correct for the purpose of the project, but I have other code that will do this part, I just want these nested for loops to "flip" the array without moving the values.
Upvotes: 0
Views: 86
Reputation: 5068
Try with:
Object[][][] containerTemp = new Object[zDim][yDim][xDim];
for (int z = 0; z < zDim; z++) {
for (int y = yDim - 1; y >= 0; y--) {
for (int x = 0; x < xDim; x++) {
containerTemp[z][y][xDim - (x + 1)] = container[z][x][y]; // change x target.
}
}
}
Upvotes: 1