0
        ArrayList<String> question = new ArrayList<String>();

        Scanner scan = new Scanner(System.in);
            for (int i=0;i<userQuestionList.size();i++){ //get questions
                if (i%2 == 0){
                    question.add(userQuestionList.get(i));
                }
            }

            for (int j = 0; j < question.size(); j++) { // get ans
                Random ran = new Random();
                System.out.print(question.remove((ran.nextInt(question.size()+1)))+" ");
                String ans = scan.nextLine();
                answer.add(ans);
            }
    }

Hi so I have this array list of questions and I want to choose it randomly then delete it one by one until there's no more so that It wont load the same question twice but there is some problem for example I have 3 questions in the question arraylist sometime it only loads 1 or two even the same question then throw me out of bound can anyone help?

3
  • It's just rnd.nextInt(question.size()). Commented Apr 26, 2022 at 1:16
  • Hi I tried that but with 3 question in the arraylist It only loads 2 of it so I am a bit confused that's why I did the -1 but it still don't work Commented Apr 26, 2022 at 1:19
  • rnd.nextInt(3) will randomly return 0, 1, or 2. Which matches exactly the sensible values to pass to the remove() method of an arraylist with 3 elements in there. "If it only loads 2", you have further problems. Commented Apr 26, 2022 at 3:05

2 Answers 2

1

Fast and simple, make use of Collections.shuffle

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");

        while (!list.isEmpty()) {
            // You don't need to do this on each iteration
            // but this is going to give you a really
            // random result
            Collections.shuffle(list);
            String value = list.remove(0);
            System.out.println(value);
        }
    }
}

First run...

e
a
c
d
b

Second run...

a
e
d
b
c

Obviously, each run will give different results

Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow I never knew there was a way this easy thanks.
Yeah, I'm lazy 😜
0

Here's a terrible example:

List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");

Random rand = new Random();

System.out.println("Original list: " + list);

int r = rand.nextInt(list.size());

System.out.println(list.get(r));

list.remove(r);

System.out.println("New list: " + list);

Console:
Original list: [a, b, c, d, e]
e
New list: [a, b, c, d]

1 Comment

Hi I tried it but when running the loop it still doesn't work it does randomize the print but I don't print all of it sometime only 2 or , 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.