下面是ArrayList的迭代器源码,我注意到这两行Object[]elementData=ArrayList.this.elementData;expectedModCount=modCount;第一行用的是标准的内部类访问外部类成员变量的方法,为何第二行则是直接用变量名,没有加“ArrayList.this”?为何modCount不需要加“ArrayList.this”?不加也能成?privateclassItrimplementsIterator{ intcursor;//indexofnextelementtoreturnintlastRet=-1;//indexoflastelementreturned;-1ifnosuchintexpectedModCount=modCount;publicbooleanhasNext(){returncursor!=size;}@SuppressWarnings("unchecked")publicEnext(){checkForComodification();inti=cursor;if(i>=size)thrownewNoSuchElementException();Object[]elementData=ArrayList.this.elementData;if(i>=elementData.length)thrownewConcurrentModificationException();cursor=i+1;return(E)elementData[lastRet=i];}publicvoidremove(){if(lastRet<0)thrownewIllegalStateException();checkForComodification();try{ArrayList.this.remove(lastRet);cursor=lastRet;lastRet=-1;expectedModCount=modCount;}catch(IndexOutOfBoundsExceptionex){thrownewConcurrentModificationException();}}
偶然的你
ABOUTYOU
相关分类