重写了啊,在ArrayList源码里面可以看到public Iterator<E> iterator() { return new Itr(); } /** * An optimized version of AbstractList.Itr */ private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {} AbstractList已经重写过了,相当于 ArrayList里面就已经有了iterator()实体了,不必再重写iterator() 只须重写AbstractList中未重写的部分
magi1201 我在ArrayList上按F3,里面源码真没有重写iterator();哦 MyEclipse为8.5 为什么你看到的ArrayList源码有 public Iterator<E> iterator() { return new Itr(); } 你是怎么看的不是按F3吗? 是不是跟 MyEclipse版本有关哦
return new Itr();
} /**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount; public boolean hasNext() {
return cursor != size;
} @SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
} public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification(); try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
} final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{}
AbstractList已经重写过了,相当于 ArrayList里面就已经有了iterator()实体了,不必再重写iterator()
只须重写AbstractList中未重写的部分
我在ArrayList上按F3,里面源码真没有重写iterator();哦 MyEclipse为8.5
为什么你看到的ArrayList源码有
public Iterator<E> iterator() { return new Itr(); }
你是怎么看的不是按F3吗? 是不是跟 MyEclipse版本有关哦