2
import java.util.Random;
import java.util.ArrayList;
public class Game {
ArrayList<Integer> numere = new ArrayList<>();
ArrayList<Bila> balls = new ArrayList<Bila>();
ArrayList<String> culori = new ArrayList<>();
Random random = new Random();
int nrBalls=0;
public void createColours(){
    for(int i=0;i<7;i++){
        culori.add("Portocaliu");
        culori.add("Rosu");
        culori.add("Albastru");
        culori.add("Verde");
        culori.add("Negru");
        culori.add("Galben");
        culori.add("Violet");
    }
}
public void createNumbers(){
    for(int i=1;i<50;i++){
        numere.add(i);
        System.out.print(numere.size());
    }
}
public void createBalls(){
    while(nrBalls<36){
        int nr =numere.get(random.nextInt(numere.size()));
        numere.remove(nr);
        String culoare =culori.get(random.nextInt(culori.size()-1));
        culori.remove(culoare);
        balls.add(new Bila(culoare,nr));
        nrBalls++;
    }
}
}

So i have another class with main method and in that class i call createNumbers() ,createColours(),createBalls().when i run the program i get an IndexOutOfBoundsException at numere.remove(nr) saying index:a number and size:another number ..always the second number is smaller than the first number..Why is this happening ?where am I wrong?

Bai Radule
  • 49
  • 2
  • 8
  • @Tunaki This doesn't look duplicated to me. The "first question" is about "What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it" and this one is about "Why sometimes an ArrayIndexOutOfBoundsException when you try to call the remove method on a List of Intergers". Is it me, or that it... They are not duplicated? – Leonardo Sibela Jun 11 '18 at 15:52

3 Answers3

2

The problem is that ArrayList.remove() has two methods, one that is an Object, and one that is an (int index). When you call the .remove with an integer, it is calling the .remove(int) which removes the index, not the object value.

In response to a comment, here is a bit more information.

The line int nr = numere.get(random.nextInt(numere.size()) returns the value of the object at the index returned by the call. The next line numere.remove(...) attempts to remove from the ArrayList the value.

You can do one of two ways:

int idx = random.nextInt(numere.size());
int nr = numere.get(idx);
numere.remove(idx);

The .remove(int) method returns the value of the remove of object, you can also do:

int idx = random.nextInt(numere.size());
int nr = numere.remove(idx);

Of course, you can consolidate those two lines into a single one if desired.

KevinO
  • 4,303
  • 4
  • 27
  • 36
  • yes i want it to remove the number in my arraylist at that position – Bai Radule Apr 14 '16 at 16:52
  • @BaiRadule, provided an updated answer with a bit more explanation. Essentially, the original code took the **value** from the array, and then attempted to remove from the index that **value** rather than the index location. – KevinO Apr 14 '16 at 17:04
  • thanks man,this is the answer i was looking for – Bai Radule Apr 14 '16 at 17:17
1

numere -- ArrayList only contains intergers 1 to 49.

numere.remove(nr); -- here nr can be any number in the range of integer. Because it was created by random function. So it is throwing an error. you can only remove the elements which are in the arraylist. else program will throw an exception

0

The remove(int) will remove the element at the given index, not the element equal to the given value. And also it returns the removed element, so you can simply do:

int nr = numere.remove(random.nextInt(numere.size()));

You can do the same for your culoare:

String culoare = culori.remove(random.nextInt(culori.size()));

Just mind that Random.nextInt(int) will throw an Exception if the argument is zero (if your List is empty).

ericbn
  • 10,163
  • 3
  • 47
  • 55
  • yes but if the random number is for ex 32 than the index to the number 32 in my array will be 31 so if i put numere.size()-1 will remove the exact number i want..but it still gives me the same error – Bai Radule Apr 14 '16 at 16:59
  • my list wont be empty becouse is taking only 35 numbers ..and my array has 49 – Bai Radule Apr 14 '16 at 17:00