Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions deepIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//***************deep iterator***************
public static class NestedCollection implements Iterable<Object>{
List<Object> objs;

NestedCollection(List<Object> list){
objs = list;
}

@Override
public Iterator<Object> iterator() {
return new NestedIterator(objs);
}

private class NestedIterator implements Iterator<Object>{

private int index;
private List<Object> objs;
private Iterator<Object> curIterator;

NestedIterator(List<Object> objs){
index = 0;
this.objs = objs;
}

@Override
public boolean hasNext() {
if(index == objs.size()) return false;
Object obj = objs.get(index);
if(obj instanceof NestedCollection){
if(curIterator != null){
if(curIterator.hasNext()){
return true;
}else{
curIterator = null;
index++;
return this.hasNext();
}
}else{
curIterator = ((NestedCollection)obj).iterator();
return this.hasNext();
}
}else{
return true;
}
}

@Override
public Object next() {
if(this.hasNext()){
Object obj = objs.get(index);
if(obj instanceof NestedCollection){
return curIterator.next();
}else{
index++;
return obj;
}
}else{
throw new RuntimeException("No next element");
}
}

}
}