2022-08-05 12:11:01 +02:00
|
|
|
class PeekingIterator implements Iterator<Integer> {
|
2024-03-02 21:01:53 +01:00
|
|
|
private Iterator<Integer> it;
|
2022-08-05 12:11:01 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
private Integer pVal;
|
|
|
|
private boolean pHasNext;
|
2022-08-05 12:11:01 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public PeekingIterator(Iterator<Integer> iterator) {
|
|
|
|
it = iterator;
|
|
|
|
next();
|
|
|
|
}
|
2022-08-05 12:11:01 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public Integer peek() {
|
|
|
|
return pVal;
|
|
|
|
}
|
2022-08-05 12:11:01 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
@Override
|
|
|
|
public Integer next() {
|
|
|
|
pHasNext = it.hasNext();
|
2022-08-05 12:11:01 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
Integer oldValue = pVal;
|
|
|
|
if (pHasNext) {
|
|
|
|
pVal = it.next();
|
2022-08-05 12:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
return oldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return pHasNext;
|
|
|
|
}
|
2022-08-05 12:11:01 +02:00
|
|
|
}
|