-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDinnerMenuIterator.java
More file actions
43 lines (34 loc) · 980 Bytes
/
DinnerMenuIterator.java
File metadata and controls
43 lines (34 loc) · 980 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
package IteratorPattern.MenuJavaUtilsExample;
import java.util.Iterator;
public class DinnerMenuIterator implements Iterator<MenuItem> {
MenuItem[] list;
int position = 0;
public DinnerMenuIterator(MenuItem[] list) {
this.list = list;
}
@Override
public MenuItem next() {
MenuItem menuItem = list[position];
position++;
return menuItem;
}
@Override
public boolean hasNext() {
if (position >= list.length || list[position] == null) {
return false;
}
return true;
}
@Override
public void remove() {
if (position <= 0) {
throw new IllegalStateException("You can't remove an item until you've done at least one next()");
}
if(list[position-1] != null){
for(int i = position-1; i < list.length-1; i++){
list[i] = list[i+1];
}
list[list.length-1] = null;
}
}
}