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

3rd Feb 2018, 2:37 PM
Manuel Neri
Manuel Neri - avatar
3 ответов
+ 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! 😉
3rd Feb 2018, 3:15 PM
Zephyr Koo
Zephyr Koo - avatar
+ 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.
3rd Feb 2018, 2:41 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 1
Thank you
3rd Feb 2018, 2:54 PM
Manuel Neri
Manuel Neri - avatar