Created
June 9, 2018 07:16
-
-
Save kenzhaoyihui/f96f86a53c46a6821327c88fae9e7312 to your computer and use it in GitHub Desktop.
java list remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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