0

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

5
  • Do you have ArrayList of ArrayList? Commented Aug 10, 2014 at 12:02
  • Just a note, the values can be "null" Commented Aug 10, 2014 at 12:04
  • 2
    This is called transposition See: stackoverflow.com/questions/2941997/how-to-transpose-listlist Commented Aug 10, 2014 at 12:06
  • so you have 16 values in an ArrayList? Commented Aug 10, 2014 at 12:13
  • I would a) use a 2D array and b) flip the index values Commented Aug 10, 2014 at 12:18

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.