Java Iterator Solution - Explained


  • 14

    I first hold the 2D List inside a Iterator of List this allows me to operate on the subsequent list once needed. I then remove the first list from the 2D List and store as row which is evaluated when next() & hasNext() are called. I then want to ensure row iterator is pointing to a none empty list so i call the getNextRow() method. next() and hashNext() are now very simple. next() returns the next element of the current list then ensures row isn't empty. hasNext() checks row isn't null and has a next value.

    public class Vector2D {
        Iterator<List<Integer>> itrs;
        Iterator<Integer> row;
        public Vector2D(List<List<Integer>> vec2d) {
            if(vec2d == null || vec2d.size() == 0) return;
            itrs = vec2d.iterator();
            row = itrs.next().iterator();
            getNextRow();
        }
        
        private void getNextRow(){
            while(!row.hasNext() && itrs.hasNext()) row = itrs.next().iterator();
        }
    
        public int next() {
            int next = row.next();
            getNextRow();
            return next;
        }
    
        public boolean hasNext() {
            return row != null && row.hasNext();
        }
    }

  • 0

    A small change of the initial method provided by leetcode
    When coding in the editor provided by leetcode in Java, the initial method name is :

    public class Vector2D implements Iterator<Integer> {
    

    I ran the previous code directly but I got

    Line 1: error: Vector2D is not abstract and does not override abstract method next() in Iterator
    

    Simply change the next() method type int into Integer to overwrite this abstract method in Iterator like:

    public Integer next() {
        hasNext();
        return j.next();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.