码迷,mamicode.com
首页 > 其他好文 > 详细

251.Flatten 2D Vector

时间:2016-06-21 10:39:24      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * 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;  
        }  
    }  

 

251.Flatten 2D Vector

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5602557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!