-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathIteratorLesson.java
More file actions
46 lines (38 loc) · 986 Bytes
/
Copy pathIteratorLesson.java
File metadata and controls
46 lines (38 loc) · 986 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
42
43
44
45
46
package patterns.behavioral;
public class IteratorLesson {
public static void main(String[] args) {
ArrayContainer arrayContainer = new ArrayContainer();
Iterator iterator = arrayContainer.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
interface Iterator {
boolean hasNext();
Object next();
}
interface Container {
Iterator iterator();
}
class ArrayContainer implements Container {
String[] array = {"Max", "Jhon", "Mikhale"};
@Override
public Iterator iterator() {
return new ArrayIterator();
}
class ArrayIterator implements Iterator {
int index;
@Override
public boolean hasNext() {
return (index < array.length) ? true : false;
}
@Override
public Object next() {
if(hasNext()) {
return array[index++];
}
return null;
}
}
}