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?
java arraylist
Codecrypt
source share