-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIterableClass.java
More file actions
41 lines (32 loc) · 1011 Bytes
/
IterableClass.java
File metadata and controls
41 lines (32 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package holding;
/**
* RUN:
* javac holding/IterableClass.java && java holding.IterableClass
* OUTPUT:
* And that is how we know the Earth to be banana-shaped
*/
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(" ");
public Iterator<String> iterator() {
return new Iterator<String>(){
private int index = 0;
public boolean hasNext() {
return index < words.length;
}
public String next() {
return words[index++];
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public static void main(String[] args) {
for (String s : new IterableClass()) {
System.out.print(s + " ");
}
System.out.println();
}
}