0

Here's my implementation of an iteratorover an ArrayList:

public class MyClass {

    List<E> list = new ArraList<E>();

public Iterator<E> iterator() {

        return new MyIterator<E>();
    }

private class MyIterator<T> implements Iterator<E> {

     int index;

        public MyIterator() {

            index = 0;

        }

        public E next() {

            if (hasNext()){
            index++;
            return list.get(index + 1);
        }

        else
            throw new NoSuchElementException();
        }

        public boolean hasNext() {

        if ((list.get(index + 1) != null) && (index < list.size() -1)){
                index++;
                return true;
            }
            else
                return false;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

}

However, although the logic looks alright to me, this implementation is wrong, because it does not work properly.

My question is: what am i doing wrong, and how can i change it in order for it to work properly?

UPDATE:

public E next() {

        if (index == 0) {
            index++;
            return list.get(0);
        }

        else if (hasNext()) {
            index++;
            return list.get(index);
        }

        else
            throw new NoSuchElementException();
    }

I've managed to, somehow, make it work, but now the next method is skipping indexes of the array. How can i fix this ?

4
  • What do you mean by "work properly"? You might find it easier to figure out what's going on if you write a unit test for your code. Commented Apr 7, 2015 at 16:11
  • What do you mean by "it does not work properly" - be specific. Also, what exactly is a PacoteIterator Commented Apr 7, 2015 at 16:11
  • @Trisha the next method returns an ArrayOutOfBoundsExceptioni don't know why Commented Apr 7, 2015 at 16:15
  • @NoseKnowsAll sorry i've edited it! translation issue! Commented Apr 7, 2015 at 16:15

1 Answer 1

1

Without an example of how you're using this iterator, it's hard to give you advice. That being said, here's at least two things you are doing incorrect. In your next() method, you have

index++;
return list.get(index + 1);

So you will first be incrementing the index, and then returning the NEXT index. This should result in an out of bounds exception. Change it to return list.get(index);

Secondly, your hasNext() method isn't ordered correctly. That is, you should check whether your next index is valid before checking whether the next object exists or not. Re-order your if statement to:

if ((index < list.size() -1) && (list.get(index + 1) != null))

Also get rid of the index++; line in hasNext(). The method should just be checking for validity, not actually changing your current index. That should happen in next().

Sign up to request clarification or add additional context in comments.

3 Comments

i'm using the iterator to navigate through the list, getting a specific element, print a specific element, that kind of operations. my issue is with the next() and hasNext() methods that are not doing what they're supposed to do
Well in that case, check my edit. I think I found 2 more things you've done incorrectly.
You should do a println to see exactly which indices are being skipped. As is, I believe the next() method is correct, so your new problem is probably in the hasNext() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.