Skip to content

Instantly share code, notes, and snippets.

@kenzhaoyihui
Created June 9, 2018 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenzhaoyihui/f96f86a53c46a6821327c88fae9e7312 to your computer and use it in GitHub Desktop.
Save kenzhaoyihui/f96f86a53c46a6821327c88fae9e7312 to your computer and use it in GitHub Desktop.
java list remove
package yzhao.blog.bean;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayTest{
public static void main (String[] args) {
List<String> list = new ArrayList<>(4);
list.add("1");
list.add("2");
list.add("2");
list.add("4");
// for (String s : list){
// if("2".equals(s)){
// list.remove(s);
// break;
// }
// }
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
String s = iterator.next();
if ("2".equals(s)){
iterator.remove();
}
}
System.out.println(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment