Collection Iterator

发布时间:2016-12-10 11:17:50 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Collection Iterator",主要涉及到Collection Iterator方面的内容,对于Collection Iterator感兴趣的同学可以参考一下。

// Collection is an interface public interface Collection<E> { boolean add(E element); Iterator<E> iterator(); ... } public interface Iterator<E> { E next(); boolean hasNext(); void remove(); } public class MergeIterator { Iterator<Integer> left; Iterator<Integer> right; int lastLeft; int lastRight; boolean leftNotUsed; public MergeIterator (Iterator<Integer> left, Iterator<Integer> right) { this.left = left; this.right = right; if (left.hasNext()) { lastLeft = left.next(); leftNotUsed = true; } else if (right.hasNext()) { lastRight = right.next(); leftNotUsed = false; } } public boolean hasNext() { return left.hasNext() | right.hasNext(); } public int next() throws Exception { if (!left.hasNext() && !right.hasNext()) { throw new Exception(" "); } if (left.hasNext() && !right.hasNext()) { if (leftNotUsed) { leftNotUsed = false; return lastLeft; } return left.next(); } if (!left.hasNext() && right.hasNext()) { if (leftNotUsed) { leftNotUsed = false; return lastLeft; } return right.next(); } if (leftNotUsed) { lastRight = right.next(); if (lastLeft < lastRight) { leftNotUsed = false; return lastLeft; } else { return lastRight; } } else { lastLeft = left.next(); if (lastRight < lastLeft) { leftNotUsed = true; return lastRight; } else { leftNotUsed = false; return lastLeft; } } } }

上一篇:
下一篇:power designer 表SQL不显示注释问题调整

相关文章

相关评论