The program I am trying to make consists of creating a class named Matrix
that is iterated with two custom iterators, rowColumIterator
and columRowIterator
. The problem is that, when initializing the iterator, in the Matrix
class, the following error occurs:
Cannot resolve method 'iterator()'
I do not know if the problem is in how I am creating the iterator or inside the Matrix class.
The iterator code is:
public class rowColumnIterator<T> implements Iterator<T> {
private T[][] dataset;
private int rowIndex;
private int columnIndex;
public rowColumnIterator(T[][] dataset) {
this.dataset = dataset;
}
@Override
public boolean hasNext() {
if (rowIndex >= dataset.length)
return false;
if (columnIndex >= dataset[rowIndex].length &&
(rowIndex >= dataset.length || rowIndex == dataset.length - 1))
return false;
return true;
}
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if (columnIndex >= dataset[rowIndex].length) {
rowIndex++;
columnIndex = 0;
}
return dataset[rowIndex][columnIndex++];
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Inside the matrix class I have this inside the constructor:
this.matrix = new int[rows][columns];
rowColumnIterator<Integer> iterator = this.matrix.iterator();
I would appreciate if you could tell my what I am doing wrong.
You have to instantiate the iterator with its constructor:
rowColumnIterator<Integer> iterator = new rowColumnIterator<>(this.matrix);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.