+ 5
Shuffling two arrays in Java
Hello, I have an assignment that creates a quiz for user. I'm supposed to shuffle the questions in a random order. I can't figure out how to shuffle the array of questions to correctly match up with the array of answers. Any help would be appreciated. Thank you.
3 Answers
+ 11
@Jacob solution works great and straightforward but I would like to offer another thought:-
If different piece of information are inter-related, combining it into a single object would be a better option with respect to OOP.
Quiz ⥠question, answer
Therefore if you have a List of Quiz, simply calling the shuffle() method from java.utils.Collection will do the job. Hopefully it helps! đ
+ 4
Letâs say there are 5 items to shuffle. You could create a third array of integers where you put random numbers from 0 to 4, and retry if there is a duplicate. Now, you can reorganize the other 2 arrays according to that order.
+ 1
Thank you