package com.iotek.myiterator;
public interface Iterator<T> {
public boolean hasNext();//Determine whether there is a next element
public T next();//Get the next element
public boolean hasNext();//Determine whether there is a next element
public T next();//Get the next element
}
package com.iotek.myiterator;
public interface List<T> {
public void add(T obj);//Add elements to the container
public T get(int index);//Get the specified position element
public int size();//Get the number of elements in the container
public Iterator<T> iterator();//Get the specific iterator object
}
public void add(T obj);//Add elements to the container
public T get(int index);//Get the specified position element
public int size();//Get the number of elements in the container
public Iterator<T> iterator();//Get the specific iterator object
}
package com.iotek.myiterator;
public class MyIterator<T> implements Iterator<T> {
private List<T> list=null;
private int index=0;//Access the current subscript of the element in the container
//When creating an iterator object, accept the iterable Container object
public MyIterator(List<T> list){
this.list=list;
}
@Override
public boolean hasNext() {
return index<list.size();
}
private List<T> list=null;
private int index=0;//Access the current subscript of the element in the container
//When creating an iterator object, accept the iterable Container object
public MyIterator(List<T> list){
this.list=list;
}
@Override
public boolean hasNext() {
return index<list.size();
}
@Override
public T next() {
return list.get(index++);
}
public T next() {
return list.get(index++);
}
}
package com.iotek.myiterator;
public class ArrayList<T> implements List<T> {
private Object [] obj=null;
private int index;
private int size;
public ArrayList(){
obj=new Object[10];
index=0;
size=0;
}
@Override
public void add(T obj) {
this.obj[index++]=obj;
size++;
}
@SuppressWarnings("unchecked")
@Override
public T get(int index) {
return (T)obj[index];
}
@Override
public int size() {
return size;
}
@Override
public Iterator<T> iterator() {
return new MyIterator<T>(this);
}
private Object [] obj=null;
private int index;
private int size;
public ArrayList(){
obj=new Object[10];
index=0;
size=0;
}
@Override
public void add(T obj) {
this.obj[index++]=obj;
size++;
}
@SuppressWarnings("unchecked")
@Override
public T get(int index) {
return (T)obj[index];
}
@Override
public int size() {
return size;
}
@Override
public Iterator<T> iterator() {
return new MyIterator<T>(this);
}
}
package com.iotek.myiterator;
public class Test {
public static void main(String[] args) {
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("zhang shan");
nameList.add("lisi");
nameList.add("wang wu");
Iterator<String> it=nameList.iterator();
while(it.hasNext())
System.out.println(it.next());
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("zhang shan");
nameList.add("lisi");
nameList.add("wang wu");
Iterator<String> it=nameList.iterator();
while(it.hasNext())
System.out.println(it.next());
}