自定义数组迭代器

/**
 * dinner 数组迭代器
 * 
 * @author Administrator
 *
 */
public class DinnerMenuIterator implements Iterator {

	private MenuItem[] menuItems;

	private Integer position = 0;

	public DinnerMenuIterator(MenuItem[] menuItems) {
		this.menuItems = menuItems;
	}

	@Override
	public boolean hasNext() {
		if (position >= menuItems.length || menuItems[position] == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Object next() {
		MenuItem menuItem = menuItems[position];
		position += 1;
		return menuItem;
	}

	@Override
	public void remove() {
		position -= 1;
		for (int i = position; i < menuItems.length - 1; i++) {
			menuItems[i] = menuItems[i + 1];
		}
		menuItems[menuItems.length - 1] = null;
	}

}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页