課程
/后端開發(fā)
/Java
/Java入門第三季
這個(gè)語句中的it是什么呢?是iterator的對(duì)象嗎?iterator是一個(gè)接口,為什么能夠聲明it呢?
2015-06-15
源自:Java入門第三季 4-6
正在回答
對(duì)于集合來說,接口中的方法都是內(nèi)部實(shí)現(xiàn)的,你可以查看AbstractList<E>中有相關(guān)聲明
?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)
簡單來說,就是集合內(nèi)部實(shí)現(xiàn)Iterator接口的方法,私有的
coursesToSelect.iterator();?
?
是的。一樣可以對(duì)象的
六代目 提問者
舉報(bào)
Java中你必須懂得常用技能,不容錯(cuò)過的精彩,快來加入吧
5 回答Iterator it=coursesToSelect.iterator()
3 回答不太理解 Iterator it=coursesToSelect.iterator() 的意思
2 回答Iterator it=coursesToSelect.iterator();這個(gè)能解釋一下么細(xì)致些
2 回答Iterator 是接口 為什么還能用it實(shí)例化呢?iterator()返回的是Itr實(shí)現(xiàn)類啊,所以接收不應(yīng)該用Itr來接收么?為什么還要用Iterator?
4 回答Course temp = (Course) coursesToSelect.get(0);
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2015-09-01
對(duì)于集合來說,接口中的方法都是內(nèi)部實(shí)現(xiàn)的,你可以查看AbstractList<E>中有相關(guān)聲明
?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();
? ? ? ? }
? ? }
簡單來說,就是集合內(nèi)部實(shí)現(xiàn)Iterator接口的方法,私有的
2015-07-25
coursesToSelect.iterator();?
?
2015-06-15
是的。一樣可以對(duì)象的