Collection Iterator

发布时间:2017-6-23 18:10:21 编辑: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不显示注释问题调整

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。