Reputation: 157
I have an ArrayList in the board game-like game I am developing in Java, I format this ArrayList as a square, as so (numbers being indexs);
/*
* |0 |1 |2 |3 |
* |4 |5 |6 |7 |
* |8 |9 |10|11|
* |12|13|14|15|
*/
What I want to do is take this ArrayList and rotate it;
e.g turn() would output (based on the original index)
/*
* |0 |4 |8 |12|
* |1 |5 |9 |13|
* |2 |6 |10|14|
* |3 |7 |11|15|
*/
My current approach is breaking the ArrayList in to rows with a getRow() function i wrote and manually writing the rows back
Upvotes: 0
Views: 644
Reputation:
for(int i = 0; i < 4; ++i){
for(int j = i; j < 4; ++j){
int t1 = al.get(i).get(j);
al.get(i).set(j, al.get(j).get(i));
al.get(j).set(i, t1);
}
}
it's called matrix transpose.
Upvotes: 1