codice:
public <S> static Iterator<S> concat(final Iterator<? extends S> a , final Iterator<? extends S> b) {
return new Iterator<Object>() {
@Override
public boolean hasNext() {
return a.hasNext() || b.hasNext();
}
@Override
public S next() {
if (a.hasNext())
return a.next();
else
return b.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
Nel main avrò una cosa tipo:
codice:
...
Iterator<S> i = concat(l.iterator(), l1.iterator());
while (i.hasNext()) {
System.out.println(i.next());
}