dinermenuiterator.java

来自「深入浅出设计模式」· Java 代码 · 共 40 行

JAVA
40
字号
package headfirst.iterator.dinermergercafe; import java.util.Iterator;  public class DinerMenuIterator implements Iterator {	MenuItem[] list;	int position = 0; 	public DinerMenuIterator(MenuItem[] list) {		this.list = list;	} 	public Object next() {		MenuItem menuItem = list[position];		position = position + 1;		return menuItem;	} 	public boolean hasNext() {		if (position >= list.length || list[position] == null) {			return false;		} else {			return true;		}	}  	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;		}	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?