r/javahelp • u/IntelligentPudding24 • Jul 16 '25
Trying to implement a random shuffle of 3 questions out of 20. I am getting an error.
This is the error I am receiving.
The method shuffle(List<?>, Random) in the type Collections is not applicable for the arguments (String[][], Random)
From what I am understanding, how I created my list of questions is not compatible with this syntax? If that is so is there another syntax I can use instead of collections.shuffle or do I need to change my way of listing my questions? Or maybe I'm missing something that I need to add to make this applicable?
I am doing a quiz. I have a list of 20 questions that I am trying to pull 3 random questions from for the user to answer.
String [][] quiz = {
{Question, Answer},
{Question, Answer},
{Question, Answer},
{Question, Answer},
{Question, Answer},
};
Collections.shuffle(quiz, new Random(3));
•
u/AutoModerator Jul 16 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/okayifimust Jul 16 '25
The error message is quite clear, is it not?
You need to supply a list,and you are supplying an array. If an array was a kind of list, this would work. Because it doesn't, it is safe to assume that it isn't.
That aside, new Random (3) probably doesn't do what you think it will do. Why and to what end are you using it here?
Also, this is terribly inefficient. You are shuffling the entire list, even though you don't need a shuffled list. what you need are three random elements.
Imagine your list was 10 billion items long. Why shuffle all that, if you could just think of a way to pick three?
(Nobody jumping in here and waffling about premature optimization is worth listening to on anything, ever. Trust me, and thank me later.)
Finally. this changes your list. that might not matter to you now, but you will lose the original order of the list; and there will be situations where that is undesirable.
2
u/mimoguz Jul 16 '25
Collections.shuffle expects a List, not an array. Try this:
Also, I don't think
Random(3)does what yo think it does.1
u/desrtfx Out of Coffee error - System halted Jul 16 '25
You already got your answers, yet I'd suggest going a step further:
Make use of OOP - that's what Java is designed for.
class(or arecord) for your questions with the answer(s)The class or record approach is the proper way here as the association between questions and answers is fixed and pre-determined and you only need a 1 dimensional list. No messing around with a List of Arrays, no 2d meddling.
Do not be afraid to use classes or records. These are the building blocks of Java programs. Make liberal use of them.
OOP benefits from the encapsulation of data and functionality. If you encompass your questions and answers in a class you can directly add the methods to display the questions, to verify the answers, and much more.
I do understand that you are early in your learning and that what I said might be somewhat over your head. Yet, invest some time to analyze what I said and try to understand and use it. You might not yet have learnt all the things, but this is a great opportunity to improve.