import java.util.Collection;
import java.util.Iterator;
/**
* 用于多个Collection连接在一起迭代时使用
* @author seamas
*
* @param <E>
*/
public class LinkedCollection<E> implements Iterable<E>{
private int pos = 0;
private int size;
private Iterator<E> pointer;
private Collection<E>[] cs;
public LinkedCollection(Collection<E> ...list ) {
size = list.length;
cs = list;
}
@Override
public Iterator<E> iterator() {
return new GoodestIterator();
}
private class GoodestIterator implements Iterator<E>{
private GoodestIterator(){
nextPointer();
}
@Override
public boolean hasNext() {
if(pointer == null) return false;
if(!pointer.hasNext()) {//当前Collection已到底
nextPointer();//移到下一iterator,NOT NULL
return hasNext();
}
return true;
}
@Override
public E next() {
return pointer.next();
}
@Override
public void remove() {
pointer.remove();
}
/**
* 返回下一个hastNext的Iterator,若已到最尾,返回null
* @return
*/
pointer = null;
}else if(cs[pos]!=null && cs[pos].iterator().hasNext()){
pointer = cs[pos].iterator();
pos++;
}else {
pos++;
nextPointer();
}
}
}
}
import java.util.Iterator;
/**
* 用于多个Collection连接在一起迭代时使用
* @author seamas
*
* @param <E>
*/
public class LinkedCollection<E> implements Iterable<E>{
private int pos = 0;
private int size;
private Iterator<E> pointer;
private Collection<E>[] cs;
public LinkedCollection(Collection<E> ...list ) {
size = list.length;
cs = list;
}
@Override
public Iterator<E> iterator() {
return new GoodestIterator();
}
private class GoodestIterator implements Iterator<E>{
private GoodestIterator(){
nextPointer();
}
@Override
public boolean hasNext() {
if(pointer == null) return false;
if(!pointer.hasNext()) {//当前Collection已到底
nextPointer();//移到下一iterator,NOT NULL
return hasNext();
}
return true;
}
@Override
public E next() {
return pointer.next();
}
@Override
public void remove() {
pointer.remove();
}
/**
* 返回下一个hastNext的Iterator,若已到最尾,返回null
* @return
*/
public void nextPointer(){
pointer = null;
}else if(cs[pos]!=null && cs[pos].iterator().hasNext()){
pointer = cs[pos].iterator();
pos++;
}else {
pos++;
nextPointer();
}
}
}
}