Aj Prieto wrote:If I have a list that contains (1, 2, 3) and I start the iterator at index 3, this is what my code outputsThe values in the [] were inputted by me as to what is expected. Is my thinking correct or are my expected values wrong?
Well, there's more than one way to do it, and it would actually be better if you showed us ALL the code for the class, especially the fields you're using.
It's also a bit difficult to understand your output, but I would remind you that
Java List and array indexes are
0-BASED.
However, the general idea is this:
If you're starting with an index that is outside the bounds (as 3 is for an array of size 3), then just treat it as 'exclusive' (ie, each call to
hasPrevious()/
previous() checks or returns the element
before the 'current' index.
So, assuming you have a 'currentIndex' field,
hasPrevious() might look something like:
return currentIndex > 0 && currentIndex <= list.size();
remove() is a bit trickier, but it's usually supposed to remove the
current element, so you generally don't need to update any indexes. The same with
set() I believe: it simply inserts a value at the current index.
The real tricky part comes in
combining hasPrevious()/
previous() and
hasNext()/
next(), but it doesn't appear that you need to worry about that yet.
HIH
Winston