codemaster
Użytkownik
- Dołączył
- Luty 26, 2013
- Posty
- 1
Witam. Mam do napisania iterator, który będzie iterował co n-ty element spośród elementów zwracanych przez iterator oryginalny. Mam takie dwa kody:
Iterator "skaczący po elementach":
Oraz iterator oryginalny:
Czy ktoś mógłby mi pomóc, ponieważ klasa nie działa.
Iterator "skaczący po elementach":
Kod:
package iteration;
public class SkipIterator implements Iterator {
private final Iterator _iterator;
private final int _skip;
SkipIterator(Iterator iterator, int skip) {
_iterator = iterator;
_skip = skip;
}
public void first() {
_iterator.first();
skipForwards();
}
public void last() {
_iterator.last();
skipBackwards();
}
public void next() {
_iterator.next();
skipForwards();
}
public void previous() {
_iterator.previous();
skipBackwards();
}
public boolean isDone() {
return _iterator.isDone();
}
public Object current() {
return _iterator.current();
}
private void skipForwards() {
for(int i = 0; i < _skip && !_iterator.isDone(); _iterator.next()) {
}
}
private void skipBackwards() {
for(int i = 0; i < _skip && !_iterator.isDone(); _iterator. previous()) {
}
}
}
Kod:
package iteration;
public class ArrayIterator implements Iterator {
private final Object[] _array;
private final int _first;
private final int _last;
private int _current;
public ArrayIterator(Object[] array) {
_array = array;
_first = 0;
_last = _array.length - 1;
_current = -1;
}
public ArrayIterator(Object[] array, int start, int length) {
_array = array;
_first = start;
_last = _first + length - 1;
_current = -1;
}
public void first() {
_current = _first;
}
public void last() {
_current = _last;
}
public void next() {
_current++;
}
public void previous() {
_current--;
}
public boolean isDone() {
return _current < _first || _current > _last;
}
public Object current() {
return _array[_current];
}
}
Czy ktoś mógłby mi pomóc, ponieważ klasa nie działa.