35 lines
1.0 KiB
Java
35 lines
1.0 KiB
Java
// collections/IterableClass.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
// Anything Iterable works with for-in.
|
|
import java.util.*;
|
|
|
|
public class IterableClass implements Iterable<String> {
|
|
protected String[] words = ("And that is how " +
|
|
"we know the Earth to be banana-shaped.").split(" ");
|
|
@Override
|
|
public Iterator<String> iterator() {
|
|
return new Iterator<String>() {
|
|
private int index = 0;
|
|
@Override
|
|
public boolean hasNext() {
|
|
return index < words.length;
|
|
}
|
|
@Override
|
|
public String next() { return words[index++]; }
|
|
@Override
|
|
public void remove() { // Not implemented
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
};
|
|
}
|
|
public static void main(String[] args) {
|
|
for(String s : new IterableClass())
|
|
System.out.print(s + " ");
|
|
}
|
|
}
|
|
/* Output:
|
|
And that is how we know the Earth to be banana-shaped.
|
|
*/
|