嗨,伙计们,这是我面试时遇到的问题,我遇到了麻烦。我很熟悉泛型/集合&迭代器,但是声明集合的方式让我感到恶心。
这里有一个问题:所提供的工作区中包含的是cocI,它是实现一个Iterator的类的开始,它可以用于迭代一个集合集合。集合集合被传递到类的构造函数中。迭代器应该首先遍历内容的深度。
例如,如果集合的外观如下所示:
[0] – [“A”, “B”, “C”]
[1] – [“D”]
[2] – [“E”, “F”] 迭代器应按以下顺序返回内容:“A”、“B”、“C”、“D”、“E”、“F”
在Q.Provide中实现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();
}
}发布于 2014-04-05 09:49:09
以下是几点开场白:
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;发布于 2014-04-05 09:09:32
您所需要做的就是跟踪集合集合中当前集合的迭代器。hasnext()方法是一个棘手的部分,它将执行两件事中的一件:如果当前迭代器有更多的元素,则返回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);
}
}
}https://stackoverflow.com/questions/22878604
复制相似问题