//迭代器 Iterator
Collection coll = Arrays.asList("123", "45", "jj", "uu", "yy");
//获取迭代器
Iterator iterator = coll.iterator();
//hasNext(); 判断有更多的元素(如果next()返回一个元素而不是抛出一个异常,则返回true)
//next();迭代中下一个元素
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
remove();方法使用
@Test
public void testRemove(){
Collection coll = Arrays.asList("123", "45", "jj", "uu", "yy");
//获取迭代器 注意:不能在调用next()方法前调用remove();
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if ("123".equals(obj)) {
iterator.remove();//删除指针指向元素
//iterator.next();//UnsupportedOperationException 元素已经被移除,再获取就为空了
}
}
}