简体   繁体   中英

Generating a random number excluding 0 from an ArrayList in java

static ArrayList<Integer> usedArray = new ArrayList<Integer>();

public static void arrayContents(){

usedArray.add(2, 2);
usedArray.add(1, 1);

}

public static void app(){

    Random generator = new Random ();

    int randomNumber = generator.nextInt(usedArray.size());

    System.out.println(usedArray);

    System.out.println(randomNumber);


    if(randomNumber == 2){
        score();
        question2();
        usedArray.remove(2);
        app();
    }

With

.add(2, 2)

I get an error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

However using

.add(2)

0 is generated as part of the array, even though it is not a value held within the array. Will arrays always contain 0 by default and is there any way of getting around this so that 0 cannot be generated as the random number ?

You are experiencing this problem because when you create the arraylist it has zero size, when you then call usedArray.add(2, 2); you are trying to update the second place in your arraylist with Integer 2; but as I say, the arraylist currently has no number 2 slot. Unless you have a reason for using the .add(index, object) method just add them normally, as .add(object)

usedArray.add(2, 2); //<-- here you update slot 2, putting Integer 2 in it, there is no slot 2 at this point

If this is simply a list of available random numbers you can just use;

usedArray.add(1);
usedArray.add(2);

Now the numbers 1 and 2 are available in the arraylist.

These can then be fetched randomly from the arraylist:

int randomNumber = usedArray.get(generator.nextInt(usedArray.size()));

In this example generator.nextInt(usedArray.size() will produce the number 0 or 1, which will fetch 1 or 2 respectively from the arraylist

The problem you have is that you create an empty List typing new ArrayList<Integer>() .

You can not use add(int index, E element) because its

Inserts the specified element at the specified position in this list.

for your list that has length equal to 0 an exception occur.

when you use add(E e) its.

Appends the specified element to the end of this list.

As Ted Hopp pointed out in comment

The problem is that you cannot insert beyond the current end of the list.

This mean you can use add(int index, E element) , only if index is lover than size.

Additionally when you call

int random = new Random().nextInt(list.size());

Then result is between 0 (inclusive) and list.size() (exclusive). This mean that you will always result some valid index and you do not have to worry about the order.

In your code you have removed 2nd position from list and trying to updating it. That's why your getting AIOBE. Use add() or try increase your list size.

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