Search code examples
javaarraylistindexoutofboundsexception

How to check if a number is between a range


I have a problem for an online course I am doing. The question is:

Given an Integer x, write a program which generates random numbers between x and 0 until each number in this range has been generated at least once. Once all numbers in this range have been generated, the program should display the numbers which were generated.

I have written a program which I thought would solve this but am having problems with the checking if a number is in the range. Here is my code so far:

    public static void main(String[] args) {
        Random generator = new Random();
        ArrayList<Integer> range = new ArrayList<Integer>();
        ArrayList<Integer> generated = new ArrayList<Integer>();

        int x = 10;
        int count = 0;

        for(int i = 0; i<x+1; i++){
            range.add(i);
        }

        while(range.isEmpty() != true){
            int temp = generator.nextInt(x-1);
            count++;
            generated.add(temp);
            if(range.contains(temp)){
                range.remove(temp);
            }
        }
    }
}

My idea was to first create two arraylists. The first would hold all numbers between 0 and the given x. The second would contain the random numbers generated. I then fill the range arraylist with the range between 0 and x. My While loop then checks this range list to see if it is empty. If not, it generates a random number, adds it to my second arraylist. I then check if this number is in the range arraylist - if it is it removes it and carries on. The problem I am having is it is running into IndexOutOfBoundsException after a few goes. I think this is because I am removing the generated numbers from the arraylist. Can anyone help me with fixing this

EDIT: I cant use any collections or other APIs. This part of the course is mainly about using Arrays and loops etc, not advanced Java stuff.


Solution

  • You can just replace range.remove(temp); with range.removeIf(t -> t == temp);

    Random generator = new Random();
            ArrayList<Integer> range = new ArrayList<Integer>();
            ArrayList<Integer> generated = new ArrayList<Integer>();
    
            int x = 10;
            int count = 0;
    
            for(int i = 0; i<x+1; i++){
                range.add(i);
            }
    
            while(range.isEmpty() != true){
                int temp = generator.nextInt(x-1);
                count++;
                generated.add(temp);
                if(range.contains(temp)){
                    range.removeIf(t -> t == temp);
                }
            }
    

    OR You can use Iterator to remove from the List

    for (Iterator<Integer> it = range.iterator(); it.hasNext(); ) {
                    Integer obj= it.next();
                    if (obj == temp) {
                        // Remove the current element from the iterator and the list.
                        it.remove();
                        break;
                    }
                }
    

    One more issue in your logic
    int temp = generator.nextInt(x-1); The random number you are generating doesn't contain all the numbers. It should be int temp = generator.nextInt(x+2);