251.Flatten 2D Vector

/*
 * 251.Flatten 2D Vector
 * 2016-6-20 by Mingyang
 *------- boolean hasNext()
 * Returns true if the iteration has more elements. 
 * (In other words, returns true if next() would return an element rather than throwing an exception.)
 *-------E next()
 *Returns the next element in the iteration.
 *Returns:
 *the next element in the iteration
 *Throws:
 *NoSuchElementException - if the iteration has no more elements
 *关键问题是掌握好Iterator的用法
 */
class Vector2D {    
  private Iterator<List<Integer>> row = null;  
  private Iterator<Integer> col = null; 
    public Vector2D(List<List<Integer>> vec2d) {  
        row = vec2d.iterator();  
        if(row.hasNext())  
            col = row.next().iterator();  
    }  
    public int next() {  
        int lastValue = col.next();  
        return lastValue;  
    }  
    public boolean hasNext() {  
        if(col == null) {  
            return false;  
        }  
        if(col.hasNext()) {  
            return true;  
        } else {  
            while(row.hasNext()) {  
                col = row.next().iterator();  
                if(col.hasNext())  
                    return true;  
            }  
            return false;  
        }  
    }  
原文地址:https://www.cnblogs.com/zmyvszk/p/5602557.html