首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >采访:收藏家

采访:收藏家
EN

Stack Overflow用户
提问于 2014-04-05 08:40:45
回答 2查看 2.3K关注 0票数 1

嗨,伙计们,这是我面试时遇到的问题,我遇到了麻烦。我很熟悉泛型/集合&迭代器,但是声明集合的方式让我感到恶心。

这里有一个问题:所提供的工作区中包含的是cocI,它是实现一个Iterator的类的开始,它可以用于迭代一个集合集合。集合集合被传递到类的构造函数中。迭代器应该首先遍历内容的深度。

例如,如果集合的外观如下所示:

代码语言:javascript
复制
[0] – [“A”, “B”, “C”] 
[1] – [“D”] 
[2] – [“E”, “F”] 

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

在Q.Provide中实现hasNext()和next()方法

谢谢

代码语言:javascript
复制
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();
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-05 09:49:09

以下是几点开场白:

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

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

代码语言:javascript
复制
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();
        }
    }

}

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

代码语言:javascript
复制
    try {
        return this.innerIterator.next();
    } finally {
        advanceCollection();
    }

它大致相当于:

代码语言:javascript
复制
    T result = this.innerIterator.next();
    advanceCollection();
    return result;
票数 0
EN

Stack Overflow用户

发布于 2014-04-05 09:09:32

您所需要做的就是跟踪集合集合中当前集合的迭代器。hasnext()方法是一个棘手的部分,它将执行两件事中的一件:如果当前迭代器有更多的元素,则返回true,如果没有搜索,直到找到包含元素的集合为止。如果我们耗尽了所有集合,则返回false。

代码语言:javascript
复制
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);
        }
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22878604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档