1

i get like 20 errors when i try to remove an int from arraylist. this is for a powerball game and im trying to remove the balls that are matched to avoid duplicate balls

for (Integer ballNumber : balls) {
   for (Integer yballNumber : yballs) {
         if (ballNumber == yballNumber) {
                match++;
                balls.remove(ballNumber);
                yballs.remove(yballNumber);
         }
    }
}



Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at PowerBall_Simulator.main(PowerBall_Simulator.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Tintinabulator Zea
  • 2,617
  • 5
  • 18
  • 32
  • There's only 1 error there. What you pasted is called a "stack trace" of the error; it shows the error and in which function it occurred, what function called that function, what function called _that_ function, etc. The error you're seeing means that you're trying to modify a list that you're iterating over. In Java you are literally not allowed to do the thing that you're asking how to do. Consider removing the balls after the loop is complete. – dg99 Jan 14 '15 at 21:21

4 Answers4

2

You can't remove elements while iterating with the enhanced for loop. You can remove via an explicit iterator.

    Iterator<Integer> ballsIt = balls.iterator();
    while (ballsIt.hasNext()) {
        Integer ballNumber = ballsIt.next();
        Iterator<Integer> yballsIt = yballs.iterator();
        boolean remove = false;
        while (yballsIt.hasNext()) {
            Integer yballNumber = yballsIt.next();
            if (ballNumber == yballNumber) {
                match++;
                remove = true;
                yballsIt.remove();
            }
        }
        if (remove)
            ballsIt.remove();
    }

Note that you shouldn't remove both balls in the inner loop, since this might result in trying to remove ballNumber from balls multiple times.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

When you're using an interator to go over a list, the list's structure must remain the same until the end of the iteration. Otherwise, the iterator will be invalidated and the programming language will not enable it to work further.

What you should do is keep a list of the indices you wish to remove, and then remove them only when the iteration is complete.

mittelmania
  • 3,393
  • 4
  • 23
  • 48
0

Iterate backwards, like this:

for (int i=balls.size()-1; i>=0 ; i--) {
      Integer ballNumber = balls.get(i);
        for (int j=yballs.size()-1; j>=0; j--) {
            Integer yballNumber = yballs.get(j);
            if (ballNumber == yballNumber) {
                match++;
                balls.remove(ballNumber);
                yballs.remove(yballNumber);

            }
        }
    }
Julian Suarez
  • 4,499
  • 4
  • 24
  • 40
  • it was working but then 1 2 3 55 55 and 4 5 6 55 55 happened and error Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 – Tintinabulator Zea Jan 15 '15 at 01:17
0

I think this may work better for you, not using an Iterator, but using a more traditional way of accessing arraylists:

for (int ballNumber = 0; ballNumber < balls.size() ; ballNumber++) {
    Integer ball = balls.get(ballnumber);
        for (int yballNumber = 0; yballNumber < yballs.size() ; yballNumber++) {
        Integer yball = yballs.get(yballnumber);
            //changed if to compare balls, not indexes
            if (ball == yball) {
                match++;
                balls.remove(ballNumber);
                yballs.remove(yballNumber);

                //you may need to adjust your indexes here
                ballnumber--;
                yballnumber--;

               if(ballnumber<0)
                   ballnumber=0;
            }
        }
}

Happy Coding! Leave a comment if you have any questions.

Matt
  • 5,404
  • 3
  • 27
  • 39