Hey
Hab volgendes Problem, das n kann ich nicht benutzen, da steht dann immer:
cannot refer to the non-final local variable defined in an enclosing scope. Leider darf ich bei der der Methodensignatur nicht das n auch noch auf final setzen.
Wie löse ich das also?
Hab volgendes Problem, das n kann ich nicht benutzen, da steht dann immer:
cannot refer to the non-final local variable defined in an enclosing scope. Leider darf ich bei der der Methodensignatur nicht das n auch noch auf final setzen.
Wie löse ich das also?
Code:
/**
* Returns an iterator that enumerates every n-th element in the collection,
* starting with the first element.
*
* @param n
* the number of skipped elements+1.
* @return the iterator.
* @throws IllegalArgumentException
* if n is less than 1.
*/
@Override
public Iterator<T> skippingIterator(int n) {
if (n < 1) {
throw new IllegalArgumentException();
} else {
Iterator<T> it = new Iterator<T>() {
private int index = 0;
private int size = list.size() - 1;
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int j = index + n;
return list.get(j);
}
@Override
public boolean hasNext() {
return index < size && list.get(index) != null ;
}
};
return it;
}
}