1:java迭代器的功能却是很强大,在便利List尤其有用,而且在删除list里不连续的相同元素效率也很高,
public class RemoveArrayListEnty { public static void main(String[] args) { List<String> list = new ArrayList<String>(); //"c"在Arraylist有连续存储 list.add("a"); list.add("c"); list.add("c"); list.add("b"); list.add("c"); list.add("c"); list.add("d"); list.add("c"); removeListElement3(list); } public static void removeListElement3(List<String> list) { Iterator<String> it = list.iterator(); while(it.hasNext()){ String str1 = it.next(); if("c".equals(str1)){ System.out.println(str1); } } //迭代器,删除list里的元素可以保证其他线程也在修改 //Iterator<String> iterator = list.iterator(); // while(iterator.hasNext()) { // String str = iterator.next(); // if("c".equals(str)) { // iterator.remove(); // } // } } }
时间: 2024-10-08 01:27:02