我正在尝试使用迭代器。我试图从集合中删除元素,以防它等于17.但它似乎打破了?谁能告诉我为什么会这样?
public class Iter {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet <Integer> treeadd = new TreeSet<Integer>();
TreeSet <Integer> remov = new TreeSet<Integer>();
treeadd.add(1);
treeadd.add(13);
treeadd.add(17);
treeadd.add(2);
remov.add(1);
Iterator<Integer> iterator = treeadd.iterator();
while(iterator.hasNext()){
Integer a = iterator.next();
if(a == 17){
iterator.remove();
}
System.out.println(iterator.next()+",");
}
}
}