IC2D
IC2D

Reputation: 501

How to return list iterator of the elements in a list

This is something i don't understand, and my professor is busy this week so i can't ask her. Can someone explain to me how to set up a list iterator, and use it to return elements in an ArrayList? thanks. It is the the method "public Iterator iterator()", i've tried my best with it, but i can't complete it.

public class ArrayList<E> implements List<E> {
private E[] elementData;
private int elementCount;
private int capacityIncrement;
private static final int INVALID_INDEX=-1;
private static final int DEFAULT_CAPACITY = 100;

public ArrayList() {

    capacityIncrement = 0;
    elementData = (E[]) new Object[DEFAULT_CAPACITY];


}

public ArrayList(int capacity) {

    this.capacityIncrement = 0;
    this.elementData = (E[]) new Object[capacity];


}

public ArrayList(int capacity, int increment) {

    this.capacityIncrement = increment;
    this.elementData = (E[]) new Object[capacity];

}

public int size() {
    return elementCount;

}

public boolean isEmpty() {
    if (elementCount != 0) return false;
    else return true;
}

public void clear() {
        elementCount = 0;

}

public boolean contains(E element) { //check back
    for (int i = 0; i < elementCount; i++) {
        if (elementData[i].equals(element)) return true; //== vs. .equals
    }
    return false;
}

public void add(E element) {
    elementCount++;
    elementData[elementCount] = element;
}

public boolean remove(E element) {
    for (int i = 0; i < elementCount; i++) { //while vs. loop
        if (elementData[i].equals(element)) {
            for (int j = 0; j <= (elementCount - i); j++){
            elementData[i] = elementData[i++];
            elementCount = elementCount - 1;
            return true;
            }


        }

    }
    return false;
}

public E elementAt(int index) {

    return elementData[index]; //elementdata vs. elementcount
}

public int indexOf(E element) {


    for (int i = 0; i < elementCount; i++) //while vs. loop
        if (elementData[i].equals(element)) {
            return i;

        } return INVALID_INDEX;


}

public void insertElementAt(E element, int index) {
    elementCount = elementCount + 1;
    for (int i = index; i < elementCount; i++) {
        elementData[i++] = elementData[i];
    }
    elementData[index] = element;
     //shift right (look at notes)
}

public void removeElementAt(int index) {
    for (int i = index; i < elementCount; i++) {
        elementData[i] = elementData[i++];

    }
    elementCount = elementCount - 1;
}

public void setElementAt(E element, int index) {

    elementData[index] = element;
}

public void removeDuplicates() {

    for (int i = 0; i < elementCount; i++) {
        for (int j = 0; j < elementCount; j++) {
            if (elementData[i].equals(elementData[j])) {
                elementData[i] = elementData[i++];
                elementCount = elementCount - 1;
            }
        }
    }
}

public void trimToSize() { //don't need to add trims to removing methods??



}

public Iterator<E> iterator() {
     Iterator itr = new list.iterator();
     while (itr.hasNext()) {

     }
    /**
 *
 * @return  a list iterator of the elements
 * in this list (in proper sequence).
 */

}

public Iterator<E> iterator(int index) {
    throw new UnsupportedOperationException("Not supported yet.");
}



private static class ArrayListIterator<E> implements Iterator<E> {
    private ArrayListIterator(ArrayList c) {
      /**
      * Returns a list iterator of the elements in this list (in proper sequence).
      * @param c list to be iterated upon
      */
        elementData=c;

    }


 }

 }

Upvotes: 0

Views: 17006

Answers (2)

John Eipe
John Eipe

Reputation: 11236

In your public Iterator iterator() method you are returning an iterator. So

public Iterator<E> iterator() {
    return this.iterator();
}

will do the job.

Upvotes: 0

John3136
John3136

Reputation: 29266

Iterator is just an interface that specifies an implementing class must provide next() and hasNext().

A simple first cut implementation would be to make your array list implement the Iterator interface.

You would add a "currentIndex" member and a method like

Iterator<E> iterator() { return this; }

next() and hasNext() would use the currentIndex and the array size as appropriate.

Obviously this breaks if you want to have more than one iterator over the same collection, so the next step would be to refactor the iterator stuff into a separate class.

Upvotes: 1

Related Questions