java - Why does the Iterator.next() method of ArrayList copy the elementData field? -


here source code of next() method in iterator provided arraylist.iterator():

public e next() {     checkforcomodification();     int = cursor;     if (i >= size)         throw new nosuchelementexception();      // why copy entire elementdata outer arraylist class?     object[] elementdata = arraylist.this.elementdata;      if (i >= elementdata.length)         throw new concurrentmodificationexception();     cursor = + 1;     return (e) elementdata[lastret = i]; } 

why code jdk try copy entire data array elementdata inner class iterator, since inner class has access fields in outer class? expensive huge list.

i know there must explanation behind code - it?

my question why jdk try copy entire data array inner class iterator. expensive huge list.

no, isn't. copies reference array, not array itself. o(1); not expensive @ all.

elementdata have accessed itr.outerclass.elementdata, outerclass being implicit reference inner class carries outer, change reduces number of indirections , references followed (by tiny amount, it's iterating on arraylist, it's 1 of common operations ever).


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -