-
迭代器是对Iterator的称呼,专门用来对Collection集合进行遍历使用的。学习迭代器的目的就是为了遍历集合
-
迭代器的使用有两个重要的接口lterable,lterator
-
Iterable接口的iterator方法
-
Iterator接口的方法
迭代器的使用步骤
Iterator it = 集合对象.iterator();
while(it.hasNext()) {
元素 = it.next();
}
迭代器的使用注意事项
- 当迭代器在使用的过程中,不能使用集合对象直接增删元素, 会导致报错CconcrrentModifcationException.
如果要删除可以使用迭代器来删除
增强for循环
- 增强fo循环(foreach) ,专门用来遍历单列集合或者数组,底层实现使用迭代器。
- 格式定义:
for(变量类型 变量: 数组/集合) {
变量代表的就是集合或数组的元素
}