icn icn - 28 days ago 19
Java Question

Implement Java Iterator and Iterable in same class?

I am trying to understand Java

Iterator
and
Iterable
interfaces

I am writing this class

class MyClass implements Iterable<String> {
public String[] a = null;
public MyClass(String[] arr) {
a = arr;
}

public MyClassIterator iterator() {
return new MyClassIterator(this);
}

public class MyClassIterator implements Iterator<String> {
private MyClass myclass = null;
private int count = 0;
public MyClassIterator(MyClass m) {
myclass = m;
}

public boolean hasNext() {
return count < myclass.a.length;
}
public String next() {
int t = count;
count++;
return myclass.a[t];
}

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


It seems to be working.

Should I have:

Myclass implements Iterable<Stirng>, Iterator<String> {

}


Or I should put
MyClassIterator
outside
MyClass
as

class MyClass implements Iterable<String> {
public String[] a = null;
public MyClass(String[] arr) {
a = arr;
}
public MyClassIterator iterator() {
return new MyClassIterator(this);
}
}


public class MyClassIterator implements Iterator<String> {
private MyClass myclass = null;
private int count = 0;
public MyClassIterator(MyClass m) {
myclass = m;
}

public boolean hasNext() {
return count < myclass.a.length;
}
public String next() {
int t = count;
count++;
return myclass.a[t];
}

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


Which one is better?

Answer

You should almost never implement both Iterable and Iterator in the same class. They do different things. An iterator is naturally stateful - as you iterate using it, it has to update its view of the world. An iterable, however, only needs to be able to create new iterators. In particular, you could have several iterators working over the same original iterable at the same time.

Your current approach is pretty much okay - there are aspects of the implementation I'd change, but it's fine in terms of the separation of responsibilities.