大家好,我收到这个面试问题,但遇到了麻烦。我熟悉泛型/集合和迭代器,但声明集合的方式完全让我困惑。
问题如下:所提供的工作区中包含 cocI,它是实现迭代器的类的开始,该迭代器可用于迭代集合的集合。集合的集合被传递到类的构造函数中。迭代器应该深度优先迭代内容。
例如,如果集合的集合如下所示:
[0] – [“A”, “B”, “C”]
[1] – [“D”]
[2] – [“E”, “F”]
迭代器应按以下顺序返回内容:“A”、“B”、“C”、“D”、“E”、“F”
Q.提供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();
}
}
您需要做的就是在集合的集合中跟踪当前集合的迭代器。
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);
}
}
}
一些介绍性的评论:
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;
好方法。我会将外迭代器作为最终变量