3

I've this problem:

Given the iterable class Foo, which keeps always only one int value which is set on its constructor, make an iterator so it respects all of its restrictions which are: you can't change the int value after its initialization. You should include only the required exceptions to be thrown.

Ok, so far, if I understood the question the right way, I should create an iterator for that Foo class, however I've never done this before and it seems to be that the question itself is a bit misleading. Is it a list? Or shouldn't it be? Anyway, despite that, all I want to know is how to create it.

So now I've this:

public class Foo implements Iterable<Foo> {
    @Override
    public Iterator<Foo> iterator() {   
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

But I don't even know if this is the right way to do so. I'd be very appreciated if someone could help me out with this.

Thank you in advance.

9
  • 1
    'Include all of the required exceptions to be thrown but only those that are needed' appears to be contradictory. Seek clarification. Commented Nov 2, 2017 at 2:44
  • The return type of the iterator() method should be Iterator<Something>. Should it be Iterator<Foo> or Iterator<Integer> or something else? Commented Nov 2, 2017 at 2:51
  • Edited. It means that I only want the needed for this class iterator restrictions and not all. I don't know if I'm saying it the right way. Commented Nov 2, 2017 at 2:52
  • Well, the exercise only tells what I quoted above, so I believe it is Iterator<Foo> ? Commented Nov 2, 2017 at 2:52
  • So what should happen when we iterate through a Foo instance? Nothing? Commented Nov 2, 2017 at 3:03

1 Answer 1

3

A minimal example would be to return an empty iterator, whose hasNext() always returns false and next() will throw NoSuchElementException.

public Iterator<Foo> iterator() {
    return new Iterator<Foo>() {
        public boolean hasNext() { 
            return false;
        }
        public Foo next() {
            throw new NoSuchElementException();
        }
    };
}

Of course most iterators have states. For example you can iterate from 0 to the integer value the Foo instance holds.

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Foo implements Iterable<Foo> {
    private final int value;

    public Foo(final int value) {
        this.value = value;
    }


    @Override
    public Iterator<Foo> iterator() {
        return new Iterator<Foo>() {
            private Foo foo = new Foo(0);

            @Override
            public boolean hasNext() {
                return foo.value < Foo.this.value;
            }

            @Override
            public Foo next() {
                if (!hasNext()) throw new NoSuchElementException();

                Foo cur = foo;
                foo = new Foo(cur.value+1);
                return cur;
            }
        };
    }

    public static void main(String[] args) {
        Foo foo = new Foo(10);
        for (Foo f: foo) {
            System.out.println(f.value);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.