課程
/后端開發
/Java
/Java入門第三季
這個語句中的it是什么呢?是iterator的對象嗎?iterator是一個接口,為什么能夠聲明it呢?
2015-06-15
源自:Java入門第三季 4-6
正在回答
對于集合來說,接口中的方法都是內部實現的,你可以查看AbstractList<E>中有相關聲明
?public Iterator<E> 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 {
? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? ? ? AbstractList.this.remove(lastRet);
? ? ? ? ? ? ? ? if (lastRet < cursor)
? ? ? ? ? ? ? ? ? ? cursor--;
? ? ? ? ? ? ? ? lastRet = -1;
? ? ? ? ? ? ? ? expectedModCount = modCount;
? ? ? ? ? ? ? ? throw new ConcurrentModificationException();
? ? ? ? final void checkForComodification() {
? ? ? ? ? ? if (modCount != expectedModCount)
簡單來說,就是集合內部實現Iterator接口的方法,私有的
coursesToSelect.iterator();?
?
是的。一樣可以對象的
六代目 提問者
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
5 回答Iterator it=coursesToSelect.iterator()
3 回答不太理解 Iterator it=coursesToSelect.iterator() 的意思
2 回答Iterator it=coursesToSelect.iterator();這個能解釋一下么細致些
4 回答Course temp = (Course) coursesToSelect.get(0);
5 回答Course cr=(Course)coursesToSelect.get(i);
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2015-09-01
對于集合來說,接口中的方法都是內部實現的,你可以查看AbstractList<E>中有相關聲明
?public Iterator<E> 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 {
? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? cursor--;
? ? ? ? ? ? ? ? lastRet = -1;
? ? ? ? ? ? ? ? expectedModCount = modCount;
? ? ? ? ? ? } catch (IndexOutOfBoundsException e) {
? ? ? ? ? ? ? ? throw new ConcurrentModificationException();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? final void checkForComodification() {
? ? ? ? ? ? if (modCount != expectedModCount)
? ? ? ? ? ? ? ? throw new ConcurrentModificationException();
? ? ? ? }
? ? }
簡單來說,就是集合內部實現Iterator接口的方法,私有的
2015-07-25
coursesToSelect.iterator();?
?
2015-06-15
是的。一樣可以對象的