How do I shuffle two arrays in same order in java

arrays, collections, java, list

Solution

You can rather shuffle a new array which holds the indices. And then get the elements from both array from the first index.

List<Integer> indexArray = Arrays.asList(0, 1, 2);

Collections.shuffle(indexArray);

String question = questions[indexArray.get(0)];
String answer = answers[indexArray.get(0)];

Of course, creating a `class` containing `questions` and `answers` would be a more OO way, as other answers suggest. That way, you would have to maintain just one `List` or `array`, as compared to `3 arrays` in the current approach.

Problem

I've got two arrays of question and answers ``` String questions[] = { "Q1?", "Q2?", "Q3?"}; String answers[] = { "A1?", "A2?", "A3?"}; ``` I used `Collections.shuffle(Arrays.asList(questions);` to shuffle each arrays. How do I shuffle each array so that after shuffling they maintain same order?

Original source