public class SkyCompany implements Company { private List<Bawcock> list = new ArrayList<>(); public SkyCompany() { list.add(new Bawcock("宋江","呼保义","天魁星","兵马大元帅")); //此处省略34行代码 list.add(new Bawcock("燕青","浪子","天巧星","步军统领兼谍报队长")); } public List<Bawcock> getList(){ return list; } @Override public Iterator iterator() { return new SkyIterator(list); } }
地煞容器:
public class LandCompany implements Company { private List<Bawcock> list = new ArrayList<>(); public LandCompany() { list.add(new Bawcock("朱武","神机军师","地魁星","军师参赞")); //此处省略70行代码 list.add(new Bawcock("段景住","金毛犬","地狗星","机密情报营头领")); } public List<Bawcock> getList(){ return list; } @Override public Iterator iterator() { return new LandIterator(list); } }
天罡迭代器:
public class SkyIterator implements Iterator { private List<Bawcock> bawcockList; private int position; public SkyIterator(List<Bawcock> bawcockList) { this.bawcockList = bawcockList; } @Override public boolean hasNext() { return !(position > bawcockList.size() - 1 || bawcockList.get(position) == null); } @Override public Object next() { Bawcock bawcock = bawcockList.get(position); position++; return bawcock; } }
地煞迭代器:
public class LandIterator implements Iterator { private List<Bawcock> bawcockList; private int position; public LandIterator(List<Bawcock> bawcockList) { this.bawcockList = bawcockList; } @Override public boolean hasNext() { return !(position > bawcockList.size() - 1 || bawcockList.get(position) == null); } @Override public Object next() { Bawcock bawcock = bawcockList.get(position); position++; return bawcock; } }