Java Implementation of Iterator Mode

Source: Internet
Author: User
package Iterator.MyTest;public interface Iterator<E> {public abstract boolean hasNext();public abstract E next();public void remove();}

package Iterator.MyTest;public interface Iterable<T> {public Iterator<T> iterator();}

 

package Iterator.MyTest;public class ArrayListIterator<E> implements Iterator<E> {int current;ArrayList<E> ce;public ArrayListIterator(ArrayList<E> ce){this.ce=ce;current=-1;}@Overridepublic boolean hasNext() {if(current+1==ce.getLength()){return false;}else{return true;}}@Overridepublic E next() {if(hasNext()){current++;return (E) ce.getObject()[current];}else{return null;}}@Overridepublic void remove() {}}

Package Iterator. myTest; import java. util. arrays; public class ArrayList <E> implements Iterable <E> {int MIN_SIZE = 10; int size; Object [] objs; Object [] temp; public ArrayList () {objs = new Object [MIN_SIZE]; size =-1 ;}@ Overridepublic Iterator <E> iterator () {return new ArrayListIterator <E> (this );} public Object [] getObject () {return objs;} public int getLength () {return size + 1;} public void add (E e) {if (size + 1 = MIN_SIZE) {MIN_SIZE + = 10;/* temp = objs; objs = new Object [MIN_SIZE]; for (int I = 0; I <temp. length; I ++) {objs [I] = temp [I];} * // ** SIMPLE METHOD **/objs = Arrays. copyOf (objs, MIN_SIZE);} objs [++ size] = e;} public boolean isEmpty () {if (size = 0) {return true ;} else {return false ;}} public E remove (int index) {E e = (E) objs [index]; for (int I = index; I <size; I ++) {objs [I] = objs [I + 1];} objs [size] = null; size --; return e;} public E get (int index) {return (E) objs [index] ;}}
Test the effects of code and jdk
Package iterator. mytest; // import Java. util. arraylist; // import Java. util. listiterator; public class mytest {/*** @ Param ARGs */public static void main (string [] ARGs) {arraylist <integer> AIS = new arraylist <integer> (); AIS. add (1); AIS. add (3); AIS. add (4); AIS. add (5); AIS. add (3); AIS. add (4); AIS. add (5); AIS. add (3); AIS. add (4); AIS. add (5); AIS. add (3); AIS. add (4); AIS. add (5); system. out. println ("deleted" + AIS. remove (2); iterator <integer> II = AIS. iterator (); While (II. hasnext () {system. out. println (II. next ();}/* arraylist <integer> AIS = new arraylist <integer> (); AIS. add (1); system. out. println ("deleted" + AIS. remove (2); listiterator <integer> II = AIS. listiterator (); While (II. hasnext () {system. out. println (II. next ());}*/}}

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.