【发布时间】:2014-12-14 18:56:39
【问题描述】:
我需要为现有的 sparsematrix 对象创建一个迭代器,但我的代码中缺少一些东西:
public Iterator<SparseMatrix.Entry<E>> iterator() {
return new EntryIterator();
}
然后:
private class EntryIterator implements Iterator<SparseMatrix.Entry<E>> {
// Possible approach:
// Consider the use of firstEntry() and higherEntry() methods
// defined in TreeMap.
Map.Entry<Position, E> mapEntry = map.firstEntry();
Iterator<E> itr = (Iterator<E>) ((SparseMatrix<E>) mapEntry).iterator() //BOLD
@Override
public boolean hasNext() {
return itr.hasNext();
}
@Override
public SparseMatrix.Entry<E> next() {
E v = itr.next();
return (pco.smatrix.SparseMatrix.Entry<E>) v;
}}
由于某种原因,我构建的迭代器无法正常工作。此外,映射引用引用了一个TreeMap 对象,然后使用该对象创建一个TMSparseMatrix,并且由于某种原因 //BOLD 行弄乱了整个代码。这是主要问题。
【问题讨论】: