简体   繁体   English

如何为2D数组/列表创建Java迭代器

[英]How to create a Java Iterator for 2D array/list

I was recently asked about the question that how to create a Java Iterator for 2D Array, specifically how to implement: 最近有人问我一个问题,即如何为2D数组创建Java迭代器,特别是如何实现:

public class PersonIterator implements Iterator<Person>{
    private List<List<Person>> list;

    public PersonIterator(List<List<Person>> list){
        this.list = list;
    }

    @Override
    public boolean hasNext() {
    }

    @Override
    public Person next() {

    }
}

1D array is pretty straightforward by using a index to track the position, any idea about how to do it for 2D lists. 一维数组通过使用索引来跟踪位置非常简单明了,有关如何对2D列表执行此操作的任何想法。

In the 1D case you need to keep one index to know where you left, right? 在1D情况下,您需要保留一个索引来知道您离开的位置,对吗? Well, in the 2D case you need two indices: one to know in which sub-list you were working, and other one to know at what element inside that sub-list you left. 好吧,在2D情况下,您需要两个索引:一个索引要知道您在哪个子列表中工作,另一个索引要知道您在该子列表中的哪个元素上留下了。

Something like this? 像这样吗 (Note: untested) (注:未经测试)

public class PersonIterator implements Iterator<Person>{
    // This keeps track of the outer set of lists, the lists of lists
    private Iterator<List<Person>> iterator;
    // This tracks the inner set of lists, the lists of persons we're going through
    private Iterator<Person> curIterator;

    public PersonIterator(List<List<Person>> list){
        // Set the outer one
        this.iterator = list.iterator();
        // And set the inner one based on whether or not we can
        if (this.iterator.hasNext()) {
            this.curIterator = iterator.next();
        } else {
            this.curIterator = null;
        }
    }

    @Override
    public boolean hasNext() {
         // If the current iterator is valid then we obviously have another one
         if (curIterator != null && curIterator.hasNext()) {
             return true;
         // Otherwise we need to safely get the iterator for the next list to iterate.
         } else if (iterator.hasNext()) {
             // We load a new iterator here
             curIterator = iterator.next();
             // and retry peeking to see if the new curIterator has any elements to iterate.
             return hasNext();
         // Otherwise we're out of lists.
         } else {
             return false;
         }
    }

    @Override
    public Person next() {
         // Return the current value off the inner iterator if we can
         if (curIterator != null && curIterator.hasNext()) {
             return curIterator.next();
         // Otherwise try to iterate along the next list and retry getting the next one.
         // This won't infinitely loop at the end since next() at the end of the outer
         // iterator should result in an NoSuchElementException.
         } else {
             curIterator = iterator.next();
             return next();
         }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM