Java using iterators


  • 0
    S
    public class Vector2D implements Iterator<Integer> {
        Iterator<List<Integer>> veciter;
        Iterator<Integer> rowiter;
        Integer next;
        public Vector2D(List<List<Integer>> vec2d) {
            veciter=vec2d.iterator();
            if(veciter.hasNext()){
                rowiter=veciter.next().iterator();
            }
            next=mynext();
        }
        
        private Integer mynext(){
            if(rowiter!=null&&rowiter.hasNext()){
                return rowiter.next();
            } else {
                while(veciter.hasNext()){
                    rowiter=veciter.next().iterator();
                    if(rowiter.hasNext()){
                        return rowiter.next();
                    }
                } 
            }
            return null;
        }
    
        @Override
        public Integer next() {
            int ret = next;
            next=mynext();
            return ret;
        }
    
        @Override
        public boolean hasNext() {
            return next!=null;
        }
    }
    

Log in to reply
 

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