游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1005|回复: 0

Java?????????Iterator??wxh zt

[复制链接]

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2004-12-23 22:41:00 | 显示全部楼层 |阅读模式
java.util????????????????????????????????????????????????????????????

??????????????Collection??????????AbstractList?????Iterator???????????????????

???????????JDK 1.4.2???JDK 1.5?java.util??????????????????????????1.4??????

???????Collection


Collection??????????????????????????

boolean add(Object c)




add()??????????????????????boolean?????????????????????doc?????Collection????????????????????????????????????????????add()??????????????????????????????????????????????????????????????????????????Collection????????

??????

boolean addAll(Collection c);
boolean remove(Object o);
boolean removeAll(Collection c);
boolean remainAll(Collection c);




Object[] toArray()?????????????????Object[] toArray(Object[] a)???????????????Object[]????????????????????????a?????????????

String[] o = (String[])c.toArray(new String[0]);




???o?????String[]?

???????a??????????????????????????????a????????????????????a??a???????????????????????a??????????????a??????????null?

???????????iterator()?????Iterator??????????????????

?Iterator????????


Iterator???????????????????????????????????????????????????????????

?????????Iterator????????????????

for(int i=0; i<array.size(); i++) { ... get(i) ... }




????????LinkedList??????while???

while((e=e.next())!=null) { ... e.data() ... }




?????????????????????????????????????????????????????????????????????????????????????

?????????????ArrayList???LinkedList?????????????????

????????Iterator????????????????

for(Iterator it = c.iterater(); it.hasNext(); ) { ... }




?????????????????"??"????????????????????????????Iterator???????Iterator????????????????????????????

????????????????????Iterator?????"??"?"??"?"?????"????????????????

????java.util.Iterator??????

public interface Iterator {
boolean hasNext();
Object next();
void remove();
}




??????????????????????

for(Iterator it = c.iterator(); it.hasNext(); ) {
Object o = it.next();
// ?o???...
}




?JDK1.5??????????????????

// Type????????String?
for(Type t : c) {
// ?t???...
}




?????????Iterator?????????Array????ArrayIterator?Set????SetIterator?Tree????TreeIterator?????????Iterator?????????????????Iterator?????????Iterator????????????????

Iterator????


??????AbstracyList????Iterator???AbstractList?????????inner class??

private class Itr implements Iterator {
...
}




?iterator()???????

public Iterator iterator() {
return new Itr();
}




???????????Iterator it = a.iterator();????Iterator??????

?????????????private?Itr????????AbstractList??

private class Itr implements Iterator {
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
}




Itr???3?int??????????AbstractList??????????cursor????next()??????????????next()??????0????lastRet??????????????????cursor?1?

??cursor??????????hasNext()?

public boolean hasNext() {
return cursor != size();
}




??next()???????cursor????????cursor?lastRet???

public Object next() {
checkForComodification();
try {
Object next = get(cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}




expectedModCount?????modCount?????????????????????AbstractList????modCount?????????0??????????????add?remove?????modCount?1????modCount????????????????

Itr?????expectedModCount?????modCount???????????????modCount???

final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}




??modCount???????expectedModeCount??????????????????????ConcurrentModificationException?

??ConcurrentModificationException?RuntimeException????????????????????????????????????????????catch?????

????Iterator???remove()??????????????????????????????expectedModCount?modCount???

public void remove() {
...
AbstractList.this.remove(lastRet);
...
// ???????remove()?????????expectedModCount?
expectedModCount = modCount;
...
}




??????????????????????????????Iterator?remove()??????????????????????????????????????????????????

??????????

Collection c = new ArrayList();
c.add("abc");
c.add("xyz");
for(Iterator it = c.iterator(); it.hasNext(); ) {
String s = (String)it.next();
System.out.println(s);
}
??????????ArrayList??LinkedList?Vector????????????????????????????????????????????????
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-23 19:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表