You are given a 2D vector vec, represented as a list of lists of integers. Design an iterator that flattens the 2D vector and iterates through all the elements in row-major order (i.e., from left to right and top to bottom).
Implement the Vector2D class:
Vector2D(int[][] vec) initializes the object with the 2D vector vec.int next() returns the next element in the flattened vector.boolean hasNext() returns true if there are still elements to be returned, or false otherwise.Your implementation should support calling next() and hasNext() multiple times and in any order.
Return the appropriate values for each operation as described above.
Need help or want more practice?