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)
1
  • 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. Commented Jan 14, 2015 at 21:21

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);

            }
        }
    }

1 Comment

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
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.

3 Comments

i tried this, when i got the numbers 3 23 37 44 59 and 3 20 33 36 45 it gave it said there were 5 matches,so for some reason it thinks 1 match is 5
ok i fixed that error balls.get was supposed to be yballs.get
still get this error when doubles pop up Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.