Question: I am trying to make a basic custom iterator within a class that simply iterates through an array but I'm having significant trouble. With what

I am trying to make a basic custom iterator within a class that simply iterates through an array but I'm having significant trouble. With what I have, my biggest trouble is the next() method; the compiler thinks the E generic is different from MyArrayList class. Any help to make this work is appreciative. Please note I have three instance variable E[] list, int size and int capacity (this variable was only used for an ensureCapacity method) if this helps thank you.

public Iteratoriterator(){ return new myIterator(); } private class myIterator implements Iterator{ private int indexPosition = 0;

public boolean hasNext(){ return indexPosition < size; } public E next(){ E e = list[indexPosition];// wrong ++indexPosition; return e; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!