Why ArrayList.remove is not working

I have the following code -

import java.util.ArrayList; public class ArrayListExp{ public static void main (String[] args){ ArrayList<String> name = new ArrayList<String>(); name.add("Chris"); name.add("Lois"); name.add("Meg"); name.add("Meg"); name.add("Brain"); name.add("Peter"); name.add("Stewie"); System.out.println(name); for ( int i = 0; i < name.size(); i++){ String oldName = name.get(i); if(oldName.equals("Meg")) { name.remove(i); } } System.out.println(name); } } 

But here he gives me a conclusion -

 [Chris, Lois, Meg, Meg, Brain, Peter, Stewie] [Chris, Lois, Meg, Brain, Peter, Stewie] 

I do not understand why this does not remove Meg , but I tried only one Meg , in this case it works. And I, when I add a little more Meg to the last, Meg not removed from the ArrayList . Why?

+8
java arraylist
source share
6 answers

When you delete the first "Meg", the index is i=2 . It then increments, but since one of the "Meg" has already been deleted, now name.get(3) is "Brain". So you didn’t actually check the second Mega.

To fix the problem. you can decrease the index when deleting an element:

 public class ArrayListExp{ public static void main (String[] args){ ArrayList<String> name = new ArrayList<String>(); name.add("Chris"); name.add("Lois"); name.add("Meg"); name.add("Meg"); name.add("Brain"); name.add("Peter"); name.add("Stewie"); System.out.println(name); for ( int i = 0; i < name.size(); i++){ String oldName = name.get(i); if(oldName.equals("Meg")) { name.remove(i); i--; } } System.out.println(name); } } 
+22
source share

You iterate over the first Meg , and when that Meg is deleted, the array values ​​are shifted by one.

 [Chris, Lois, Meg, Meg, Brain, Peter, Stewie] 0 1 2 3 4 5 6 

First, Meg is deleted, and the loop increments i, because it completed everything inside the for loop, so i will now be 3, and the array has been changed:

 [Chris, Lois, Meg, Brain, Peter, Stewie] 0 1 2 3 4 5 

Try a repeat. back.

 for ( int i = name.size() - 1; i >= 0; i--){ String oldName = name.get(i); if(oldName.equals("Meg")) { name.remove(i); } } 
+4
source share

You can use name.removeAll(Arrays.asList("Meg")); to delete all "Meg"

Your complete code will be

 for ( int i = 0; i < name.size(); i++){ String oldName = name.get(i); if(oldName.equals("Meg")) { name.removeAll(Arrays.asList("Meg")); } } 
+3
source share

You remove from ArrayList, iterate over it from 0 to N, so when you delete the first Meg with index N, the next Meg goes down to index N, then you increase me to N + 1. So the 2nd Mega is not deleted. Try iterating in reverse order (from N to 0):

 for ( int i = name.size() - 1; i >= 0; i--) { 
+1
source share

Because when i = 2, and if the condition is true, then meg is deleted and all indices are shifted up. so next I’ll point to the Brain, not the mega.

try it. (reduce me by one if the condition is met)

 for ( int i = 0; i < name.size(); i++){ String oldName = name.get(i); if(oldName.equals("Meg")) { name.remove(i); i--; } } 
+1
source share

When deleting elements you should not use for a loop. This always creates problems when implementing some logic. Use loop feedback for your problem and always try to use one for everyone.

0
source share

All Articles