为什么用Iteator it = coll.iterator(); 会用,不理解
1.因为Iterator 是接口 接口不能实例化 而coll对象中的 hasnext(),next()在API中并没有显示implements Itreator 也没有重写hasnext();next(),
2. ComperTo方法需要重写才能比较 为什么hasnext(),next()不需要重写呢
3. 如果我自己定义个Arr类implements Iterator 我需要重写hasnext,next方法吗 next方法返回类型应该是什么
4.谢谢回答 iterator
1.因为Iterator 是接口 接口不能实例化 而coll对象中的 hasnext(),next()在API中并没有显示implements Itreator 也没有重写hasnext();next(),
2. ComperTo方法需要重写才能比较 为什么hasnext(),next()不需要重写呢
3. 如果我自己定义个Arr类implements Iterator 我需要重写hasnext,next方法吗 next方法返回类型应该是什么
4.谢谢回答 iterator
return new Itr();
} 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 {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
} public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification(); try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
} final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}