亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Iterator<Course> it = coursesToSelect.iterator();

這個語句中的it是什么呢?是iterator的對象嗎?iterator是一個接口,為什么能夠聲明it呢?

正在回答

3 回答

對于集合來說,接口中的方法都是內部實現的,你可以查看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接口的方法,私有的

0 回復 有任何疑惑可以回復我~

coursesToSelect.iterator();?

?

0 回復 有任何疑惑可以回復我~

是的。一樣可以對象的

0 回復 有任何疑惑可以回復我~
#1

六代目 提問者

能具體解釋一下嗎
2015-06-15 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

Iterator<Course> it = coursesToSelect.iterator();

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號