How does JDK implement the iterator interface?

Source: Internet
Author: User
Tags concurrentmodificationexception

The following code is used by enthusiastic netizens to implement the iterator interface in the form of anonymous classes:

Import Java. util. iterator; <br/> public class ourarraylist <E> implements iterable <e >{< BR/> private e [] datas; <br/> private int size; </P> <p> Public ourarraylist () {<br/> This (16); <br/>}</P> <p> @ suppresswarnings ("unchecked ") <br/> Public ourarraylist (INT capacity) {<br/> datas = (E []) New object [capacity]; <br/> size = 0; <br/>}</P> <p> @ suppresswarnings ("unchecked") <br/> Public void add (INT index, e OBJ) {<br/> If (size> = datas. length) {<br/> E [] newdata = (E []) New object [datas. length * 2 + 1]; <br/> system. arraycopy (datas, 0, newdata, 0, datas. length); <br/> datas = newdata; <br/>}</P> <p> for (INT I = size-1; I >= index; I --) <br/> datas [I + 1] = datas [I]; <br/> datas [Index] = OBJ; <br/> size ++; <br/>}</P> <p> Public e get (INT index) {<br/> If (index <size) {<br/> return (E) datas [Index]; <br/>}else <br/> return NULL; <br/>}</P> <p> Public iterator <E> iterator () {<br/> // anonymous internal class implementing the iterator interface <br/> return New iterator <E> () {<br/> int COUNT = 0; </P> <p> Public Boolean hasnext () {<br/> return count <size; <br/>}</P> <p> Public E next () {<br/> return get (count ++); <br/>}</P> <p> Public void remove () {<br/> // todo auto-generated method stub </P> <p >}</P> <p> }; <br/>}</P> <p>

In fact, JDK implements the iterator interface in this way. The following code is extracted from the javasactlist.

Private class itr implements iterator <e >{< br/>/** <br/> * index of element to be returned by subsequent call to next. <br/> */<br/> int cursor = 0; </P> <p>/** <br/> * index of element returned by most recent call to next or <br/> * previous. reset to-1 if this element is deleted by a call <br/> * to remove. <br/> */<br/> int lastret =-1; </P> <p>/** <br/> * The modcount value that the iterator bel Ieves that the backing <br/> * List shocould have. if this expectation is violated, the iterator <br/> * has detected concurrent modification. <br/> */<br/> int expectedmodcount = modcount; </P> <p> Public Boolean hasnext () {<br/> return cursor! = Size (); <br/>}</P> <p> Public E next () {<br/> checkforcomodification (); <br/> try {<br/> E next = get (cursor); <br/> lastret = cursor ++; <br/> return next; <br/>} catch (indexoutofboundsexception e) {<br/> checkforcomodification (); <br/> throw new nosuchelementexception (); <br/>}</P> <p> Public void remove () {<br/> If (lastret =-1) <br/> throw new illegalstateexception (); <br/> checkforcomod Ification (); </P> <p> try {<br/> abstractlist. this. remove (lastret); <br/> If (lastret <cursor) <br/> cursor --; <br/> lastret =-1; <br/> expectedmodcount = modcount; <br/>} catch (indexoutofboundsexception e) {<br/> throw new concurrentmodificationexception (); <br/>}</P> <p> final void checkforcomodification () {<br/> If (modcount! = Expectedmodcount) <br/> throw new concurrentmodificationexception (); <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.