访谈:收藏迭代者

时间:2014-04-05 08:40:45

标签: java collections iterator

大家好,我把这作为一个面试问题并且遇到了麻烦。我熟悉仿制品/集合&迭代器但是声明Collection的方式完全让我感动。

接下来的问题:提供的工作空间中包含cocI,它是实现Iterator的类的开头,可用于迭代集合集合。集合集合被传递到类的构造函数中。迭代器应该深度遍历内容深度优先。

例如,如果集合集如下所示:

[0] – [“A”, “B”, “C”] 
[1] – [“D”] 
[2] – [“E”, “F”] 

迭代器应按以下顺序返回内容:“A”,“B”,“C”,“D”,“E”,“F”

问:为cocI

中的hasNext()和next()方法提供实现

由于

import java.util.Collection;
import java.util.Iterator;

public class cocI implements Iterator<Object> {

    private Collection<Collection<Object>> _collOfColl = null;

    public cocI(Collection<Collection<Object>> collofColl) {
        _collOfColl = collofColl;
    }

    public boolean hasNext() {
        // TODO implement this method
        return false;
    }


    public Object next() {
        // TODO implement this method
        return null;
    }


    public void remove() {
        throw new UnsupportedOperationException();
    }

}

2 个答案:

答案 0 :(得分:2)

您需要做的就是跟踪集合集合中当前集合的迭代器。 hasnext()方法是棘手的部分,然后将执行以下两种操作之一:如果当前迭代器具有更多元素,则返回true;如果找不到具有元素的集合,则返回true。如果我们耗尽所有集合,则返回false。

public class Cocl implements Iterator<Object> {

    private Collection<Collection<Object>> _collOfColl = null;
    private final Iterator<Collection<Object>> coClIterator;
    private Iterator<Object> currentColIterator;

    public Cocl(Collection<Collection<Object>> collofColl) {
        _collOfColl = collofColl;
        coClIterator = collofColl.iterator();
        if (coClIterator.hasNext()) {
            currentColIterator = coClIterator.next().iterator();
        }
    }

    public boolean hasNext() {
        if (currentColIterator == null) {
            return false;
        }
        if (!currentColIterator.hasNext()) {
            while (coClIterator.hasNext()) {
                currentColIterator = coClIterator.next().iterator();
                if (currentColIterator.hasNext()) {
                    return true;
                }
            }
            return false;
        } else {
            return true;
        }
    }

    public Object next() {
        if (hasNext()) {
            return currentColIterator.next();
        }
        throw new NoSuchElementException();
    }


    public void remove() {
        throw new UnsupportedOperationException();
    }

    public static void main(String[] args) {
        Collection<Object> one = Arrays.asList((Object) "A", (Object) "B", (Object) "C");
        Collection<Object> two = Arrays.asList((Object) "D", (Object) "E");
        Cocl cocl = new Cocl(Arrays.asList(one, two));
        while (cocl.hasNext()) {
            Object a = cocl.next();
            System.out.println(a);
        }
    }

}

答案 1 :(得分:0)

一些介绍性的评论:

  • cocI是一个奇怪的类名;它应该以大写字母开头。
  • 您应该实现的接口不会有效地使用泛型。您应该能够使用比Object更具体的数据类型。
  • 最好使用@Override注释。

该解决方案涉及外部集合的迭代器和内部集合的迭代器。当内部迭代器用完元素时,需要用迭代器替换下一个集合。但是,考虑到集合可能是空的,需要在循环中完成进度,我已将其放在advanceCollection()帮助器中。

import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class cocI<T> implements Iterator<T> {

    private Iterator<Collection<T>> outerIterator;
    private Iterator<T> innerIterator;

    public cocI(Collection<Collection<T>> collofColl) {
        this.outerIterator = collofColl.iterator();
        advanceCollection();
    }

    @Override
    public boolean hasNext() {
        return this.innerIterator != null && this.innerIterator.hasNext();
    }

    @Override
    public T next() {
        if (this.innerIterator == null) {
            throw new NoSuchElementException();
        }
        try {
            return this.innerIterator.next();
        } finally {
            advanceCollection();
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }

    private void advanceCollection() {
        while ((this.innerIterator == null || !this.innerIterator.hasNext())
               && this.outerIterator.hasNext()) {
            this.innerIterator = this.outerIterator.next().iterator();
        }
    }

}

我使用了一个稍微棘手的代码:

    try {
        return this.innerIterator.next();
    } finally {
        advanceCollection();
    }

大致相当于:

    T result = this.innerIterator.next();
    advanceCollection();
    return result;