-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedIterator.java
More file actions
55 lines (44 loc) · 949 Bytes
/
NestedIterator.java
File metadata and controls
55 lines (44 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class NestedIterator implements Iterator<Integer>
{
int _count, _step;
List<NestedInteger> _nestedList;
public NestedIterator( List<NestedInteger> nestedList )
{
_nestedList = new ArrayList<>();
Integerflat( nestedList );
_count = _nestedList.size();
_step = 0;
}
public void Integerflat( List<NestedInteger> nestedList )
{
for ( NestedInteger n : nestedList )
{
if ( n.isInteger() )
_nestedList.add( n );
else
{
Integerflat( n.getList() );
}
}
}
@Override
public Integer next()
{
Integer ret = _nestedList.get( _step ).getInteger();
_step++;
return ret;
}
@Override
public boolean hasNext()
{
return _count > _step;
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/