网站首页 > java教程 正文
1 迭代器
1.1 Iterator的API
public interface Iterator<E> {
/*** Returns {@code true} if the iteration has more elements.*/
boolean hasNext();
/*** Returns the next element in the iteration. */
E next();
/**
* Removes from the underlying collection the last element returned
* by this iterator (optional operation). This method can be called
* only once per call to {@link #next}. The behavior of an iterator
* is unspecified if the underlying collection is modified while the
* iteration is in progress in any way other than by calling this
* method.
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
1.2 ArrayList的iterator()方法
public Iterator<E> iterator() {
return listIterator();
}
public ListIterator<E> listIterator() {
return listIterator(0);
}
public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index);
return new ListItr(index);
}
private class Itr implements Iterator<E> {
/**Index of element to be returned by subsequent call to next. */
int cursor = 0;
/** * Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove. */
int lastRet = -1;
/** * The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();重要,放回是否有未被遍历的元素
}
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1; 重要,游标后移
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);重要,移除上一个
if (lastRet < cursor) next后连续调用2次remove?
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException(); 重要
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
重要,并发修改异常
throw new ConcurrentModificationException();
}
}
1.3 遍历List
public static void main(String[] args) {
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
//通过索引遍历List
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
//通过迭代器遍历List
for(Iterator iter2 = list.iterator();iter2.hasNext();){
String str = (String) iter2.next();
System.out.println(str);
iter2.remove();
iter2.remove();
}
Set set = new HashSet();
set.add("高1");
set.add("高2");
set.add("高3");
//通过迭代器遍历Set
// Iterator iter = set.iterator();
// while(iter.hasNext()){
for(Iterator iter = set.iterator();iter.hasNext();){
String str = (String) iter.next();
System.out.println(str);
}
1.4 遍历Map
public static void main(String[] args) {
Map map = new HashMap();
map.put("aa", "aaaa");
map.put("bb", "bbbb");
//遍历Map的第一种方式,先获取所有的键,根据键找值
Set keys = map.keySet();
for(Iterator iter = keys.iterator();iter.hasNext();){
String keyStr = (String) iter.next();
System.out.println(keyStr+"---"+map.get(keyStr));
}
//遍历Map的第二种方式,先获取所有的键值对,键值对直接获取值
Set<Entry> set2 = map.entrySet();
for(Iterator iter = set2.iterator();iter.hasNext();){
Entry e = (Entry) iter.next();
System.out.println(e.getKey()+"---"+e.getValue());
}
}
1.5 并发修改异常
运行结果:
API中认识一下这个异常:
2种解决办法:
猜你喜欢
- 2024-09-14 Java手机验证码发送及验证(java手机验证码怎么实现)
- 2024-09-14 移动开发者最爱的9个优秀Android代码编辑器
- 2024-09-14 边玩手机边学Java---Java基础之TreeMap、TreeSet实现原理
- 2024-09-14 我翻出了压在箱底的OPPO翻盖手机,重温JavaQQ和游戏
- 2024-09-14 可以在手机上进行Java,Python的编程软件,你用过么?
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)