|
|
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???????????????????????????????????????????????? |
|