Creating an ArrayList iterator from scratch

I am having trouble understanding how iterators are supposed to be implemented in a working program. I am trying to make an ArrayList that I can iterate through and supports wrap-around. I'm not sure if the methods are one-hundred percent correct since I can't even get a working driver to test them with. Here's what I have so far.

publicinterface Iterator<E>{

publicboolean hasNext();

public E next();

publicvoid remove();

}

publicinterface ListIterator<E>extends Iterator<E>{

publicvoid add(E e);

publicboolean hasNext();

publicboolean hasPrevious();

public E next();

publicint nextIndex();

public E previous();

publicint previousIndex();

publicvoid remove();

publicvoid set(E e);

}

publicclass ArrayIterator<E>implements ListIterator<E>{

protected E[] a;

protectedint size;

protectedint cursor;

protectedint lastIndex;

public ArrayIterator(){

this.a = a;

this.size = size;

cursor = -1;

lastIndex = -1;

}

publicvoid add(E e){

thrownew UnsupportedOperationException("Size of array is fixed" );

}

publicboolean hasNext(){

return size > 0;

}

publicboolean hasPrevious(){

return size > 0;

}

public E next(){

if ( cursor == -1 || cursor == size )

cursor = 0;

lastIndex = cursor;

return a[cursor++];

}

publicint nextIndex(){

int i = lastIndex + 1;

if ( i == size )

i = 0;

return i;

}

public E previous(){

if ( cursor <= 0 )

cursor = size;

lastIndex = --cursor;

return a[cursor];

}

publicint previousIndex(){

int i = lastIndex - 1;

if ( i < 0 )

i = size - 1;

return i;

}

publicvoid remove(){

thrownew UnsupportedOperationException("Size of array is fixed" );

}

publicvoid set(E e){

a[lastIndex] = e;

}

public Iterator<E> iterator(){

returnthis;

}

}

publicclass ArrayIndexList<E>implements IndexList<E>{

private E[] A;

privateint capacity = 16;

privateint size = 0;

privateboolean usedNext =false;

public ArrayIndexList(){

A = (E[])new Object[capacity];

}

publicvoid add(int r, E e){

if (size == capacity){

capacity *= 2;

E[] B =(E[])new Object[capacity];

for (int i = 0; i < size; i++)

B[i] = A[i];

A = B;

}

for (int i = size-1; i >= r; i--)

A[i+1] = A[i];

A[r] = e;

size++;

}

public E get(int i){

return A[i];

}

publicboolean isEmpty(){

return ( size == 0 );

}

public Iterator iterator(){

returnnew ArrayIterator();

}

public E remove(int r){

E temp = A[r];

for (int i = r; i < size-1; i++)

A[i] = A[i+1];

size--;

return temp;

}

publicvoid set(int i, E e){

A[i] = e;

}

publicint size(){

return size;

}

public String toString(){

Iterator<E> it = this.iterator();

String s ="[";

while (it.hasNext()){

s += it.next();

if (it.hasNext())

s +=", ";

}

s +="]";

return s;

}

}

If someone can tell me what I'm doing wrong, or can give/link me an example of a fully functioning ArrayList iterator with all of the classes, then that'd be great. I'm sorry I can't be any more specific with what I'm stuck on. I don't know even know which questions I should be asking. :(

[9159 byte] By [ShiggyMotoa] at [2007-11-27 6:15:23]
# 1
you don't need to implement Iterator to using them. What you get from your ArrayList is a implemented Iterator.
j_shadinataa at 2007-7-12 17:25:55 > top of Java-index,Core,Core APIs...
# 2

example:

Iterator<String> i = anArrayList.iterator();

while (i.hasNext()) {

String aString = i.next();

// ...

}

in java 1.4 i often use iterator like this:

for (Iterator i = aList.iterator(); i.hasNext(); ) {

Object o = e.next();

}

j_shadinataa at 2007-7-12 17:25:55 > top of Java-index,Core,Core APIs...