By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].
Follow up: As an added challenge, try to code it using only iterators in C++ or iterators in Java.
Solution
publicclassVector2DimplementsIterator<Integer> {List<Integer> integers;Iterator<Integer> iterator;publicVector2D(List<List<Integer>> vec2d) { integers =vec2d.stream().flatMap(item->item.stream()).collect(Collectors.toList()); iterator =integers.iterator(); } @OverridepublicIntegernext() {returniterator.next(); } @OverridepublicbooleanhasNext() {returniterator.hasNext(); }}/** * Your Vector2D object will be instantiated and called as such: * Vector2D i = new Vector2D(vec2d); * while (i.hasNext()) v[f()] = i.next(); */