简体   繁体   中英

Checking if number already exists

I am generating a random number between 40 and 50. Once the number is generated I print it to the screen and I add it to a ArrayList . However if the number already exists in the ArrayList then I print a message "This number already exists in Array List" . So the code should never print the same number twice. However for some reason it keeps printing the same number twice even when it exists in the array list. I'm not sure what I am doing wrong here

import java.util.ArrayList;
import java.util.Random; 

public class RandomNumber {

    Random random = new Random(); 
    int x;
    int l;
    public static void main(String[] args) {
        RandomNumber r = new RandomNumber();
        r.genNumber(50,40);
    }

    public void genNumber(int high, int low) {
        x = random.nextInt(high-low)+low;
        ArrayList<Integer> arl = new ArrayList<Integer>();


        if(!arl.contains(x)) {
            System.out.println(x);
            arl.add(x);

        } else if (arl.contains(x)) {
            System.out.println("This is already in ArrayList");
        }
    }
}

The logic in your code is broken.

First, when you check if(!arl.contains(x)) , arl is empty, so this condition is always true.

More importantly, if you want to maintain just unique numbers, use a Set instead of a List . And this set should be global , ie, declared outside the genNumber method. The code below should work (untested, and I kept your original data structures).

import java.util.ArrayList;
import java.util.Random; 

public class RandomNumber {

    Random random = new Random(); 
    int x;
    int l;
    ArrayList<Integer> arl;

    public RandomNumber() {
        arl = new ArrayList<>();
    }

    public static void main(String[] args) {
        RandomNumber r = new RandomNumber();
        r.genNumber(50,40);
    }

    public void genNumber(int high, int low){
        x = random.nextInt(high-low)+low;
        if(!arl.contains(x)) {
            System.out.println(x);
            arl.add(x);
        } else {
            System.out.println("This is already in ArrayList");
        }
    }
}

You need to make the ArrayList a field, instead of a local variable. This way it can be reused between method calls (as long as you use the same instance of RandomNumber). In your current implementation, the ArrayList that keeps track of already generated numbers is thrown away at the end of the method.

This works:

import java.util.ArrayList;
import java.util.Random;

public class RandomNumber {

    Random random = new Random();
    int x;
    int l;
    ArrayList<Integer> arl = new ArrayList<Integer>();

    public static void main(String[] args) {
        RandomNumber r = new RandomNumber();
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);

        //r = new RandomNumber(); // created a new one - uncommenting this line will mean the List is lost and we will generate duplicates.
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
        r.genNumber(50,40);
    }

    public void genNumber(int high, int low){
        x = random.nextInt(high-low)+low;

        if(!arl.contains(x)){
            System.out.println(x);
            arl.add(x);

        }else if(arl.contains(x)){
            System.out.println("This is already in ArrayList");
        }
    }
}

Actually I think you're misunderstanding the error. You are making a brand new ArrayList every time you call your method, so it always starts out empty. So, no problem adding the random number to it.

To fix, you'll want to move your arl out of that method so that it is created once and lives on until your app is done (or until you're done storing random numbers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM