Slicktopher
Slicktopher

Reputation: 137

Java - Implementing a custom Iterable without using an inner-iterator class

I have the follow code

public class SBag<Item> implements BagInterface<Item>, Iterable<Item> {

And when I try to compile I get

SBag.java:12: error: SBag is not abstract and does not override abstract method
iterator() in Iterable
public class SBag<Item> implements BagInterface<Item>, Iterable<Item>{
       ^
 where Item is a type-variable:
Item extends Object declared in class SBag

My task is to implement Iterable without using an inner-iterator class, but I am unsure of how to do this because I get that error when compiling. I have the followin methods add(), isFull(), toArray(), isEmpty(), getCurrentSize(), remove(), clear(), and toString(). The overall goal is to be able to use a for-each loop, but I am unsure of how to proceed from here.

Upvotes: 2

Views: 5782

Answers (3)

ZX9
ZX9

Reputation: 977

While [this answer] provides the regular syntax for Iterable implementations, an Iterator can be useful without an Iterable-implementing class. For example:

public class DoesntIterate{

public void coolMethod(){
//Do stuff
Iterator iter = getMyIterator();
   while(iter.hasNext()){
   //Do stuff with iter.next()
   }
}

private Iterator getMyIterator(){
   return new MyIterator();
}

private class MyIterator implements Iterator{
...
}

}

With this sort of paradigm, it's conceivable that you might use different iterators for different purposes all within the same class.

From an OOP perspective, you should never be making a class implement Iterable when it doesn't make sense for that to be an class on which you would iterate (i.e. if the class is not a data/storage structure).

Upvotes: 0

Ryan Stewart
Ryan Stewart

Reputation: 128909

An Iterator as an inner class would look like this:

class MyIterable implements Iterable {
    public Iterator iterator() {
        return new Iterator() {
            public boolean hasNext() {...}
            public Object next() {...}
            void remove();
        }
    }
}

In contrast, an Iterator that's not an inner class might look more like:

class MyIterable implements Iterable {
    public Iterator iterator() {
        return new MyIterator();
    }
}

class MyIterator {
    public boolean hasNext() {...}
    public Object next() {...}
    void remove();
}

This is another way that's technically speaking not an inner class, but some people will look at you funny if you say that:

class MyIterable implements Iterable {
    public Iterator iterator() {
        return new MyIterator();
    }

    static class MyIterator {
        public boolean hasNext() {...}
        public Object next() {...}
        void remove();
    }
}

Upvotes: 2

Maroun
Maroun

Reputation: 95998

When you implement Iterable, you can then use for:each loop syntax:

Implementing this interface allows an object to be the target of the "foreach" statement.

Iterable is a generic interface, you should implement the method it contains:

public class MyIterable<E> implements Iterable<E>{
    public Iterator<E> iterator() {    // <--- Implement me!
        return new CustomIterator<E>();
    }
}

And then, for example, you can do something like this:

public class CustomIterator<T> implements Iterator<T> {
    public boolean hasNext() {
         //...
    }

    public T next() {
        //...
    }

    public void remove() {
        //...
    }
}

Upvotes: 1

Related Questions