Skip to content

Instantly share code, notes, and snippets.

@mahata
Last active January 1, 2019 14:35
Show Gist options
  • Select an option

  • Save mahata/7d95697cca72e7201266332e029adefc to your computer and use it in GitHub Desktop.

Select an option

Save mahata/7d95697cca72e7201266332e029adefc to your computer and use it in GitHub Desktop.
How remove() works in loops
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
for (String str : list) {
if ("B".equals(str)) {
list.remove(str);
} else {
System.out.println(str);
}
}
}
}
// Result: A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment