10

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?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Sujal
  • 671
  • 1
  • 16
  • 34
  • possible duplicate of [how to shuffle two list in the same fashion in java?](http://stackoverflow.com/questions/10312442/how-to-shuffle-two-list-in-the-same-fashion-in-java) – assylias Nov 23 '12 at 16:43
  • 3
    The link proposes a better approach, from an OOP perspective, which consists in storing the questions and answers together. Then you only have one array or list which is easy to shuffle. – assylias Nov 23 '12 at 16:44
  • though https://stackoverflow.com/a/44863528/1815624 is a great way to shuffle 2 lists together it does not solve the array problem for that try my answer https://stackoverflow.com/a/52486912/1815624 – CrandellWS Sep 24 '18 at 20:32

6 Answers6

12

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
8

Creating a class for holding both the question and answer together would be an easier and more OO solution:

class QuestionAnswerPair {
    private final String question;
    private final String answer;

    public QuestionAnswerPair(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }
}

And then:

QuestionAnswerPair[] questions = new QuestionAnswerPair[] {
    // Put questions here
};

Collections.shuffle(Arrays.asList(questions));
Phil K
  • 4,939
  • 6
  • 31
  • 56
6

Create a class QuestionAndAnswer and use an array of that class.

jlordo
  • 37,490
  • 6
  • 58
  • 83
3

Java Collections has a (surprisingly) simple solution to this problem: Collections.shuffle(Collection<?>, Random) with a Random seeded with same seed.

    List<Integer> quests = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> answers = Arrays.asList(10, 20, 30, 40, 50);

    long seed = System.nanoTime();
    Collections.shuffle(quests, new Random(seed));
    Collections.shuffle(answers, new Random(seed));

    System.out.println(quests);
    System.out.println(answers);

Note:

Extra optimization is dangerous. This DOE NOT WORK:

    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    Collections.shuffle(quests, rnd);
    Collections.shuffle(answers, rnd);

Originally posted at: https://stackoverflow.com/a/44863528/1506477

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
2

Instead of shuffling answers and questions, you may shuffle an extra array of integers that has indexes to questions/answers and then extract question and answers from corresponding arrays using shuffled indexes.

Muhammad Tauseef
  • 413
  • 2
  • 6
  • 18
0

Idea from: http://www.vogella.com/tutorials/JavaAlgorithmsShuffle/article.html

public static void shuffle2ArraysTogther(String[] a, String[] b) {
    if(a.length == b.length) {
        int n = a.length;
        Random random = new Random();
        random.nextInt();
        for (int i = 0; i < n; i++) {
            int change = i + random.nextInt(n - i);
            swap(a, i, change);
            swap(b, i, change);
        }
    }
}

private static void swap(String[] a, int i, int change) {
    String helper = a[i];
    a[i] = a[change];
    a[change] = helper;
}

private static void swap(String[] a, int i, int change) {
    String helper = a[i];
    a[i] = a[change];
    a[change] = helper;
}
String questions[] = {
    "Q1?",
    "Q2?",
    "Q3?"
};

String answers[] = {
    "A1?",
    "A2?",
    "A3?"
};
shuffle2ArraysTogther(questions, answers);
for (String i : questions) {
    System.out.println(i);
}
for (String i : answers) {
    System.out.println(i);
}
CrandellWS
  • 2,708
  • 5
  • 49
  • 111