Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class Frog implements Iterator<Integer> {
- private int cursor = elements.size() == 0 ? 0 : -2;
- private boolean isEvenCursor() {
- return cursor % 2 == 0;
- }
- @Override
- public boolean hasNext() {
- if (isEvenCursor() && elements.size() > 2) {
- return true;
- } else if (!isEvenCursor() && elements.size() > 2) {
- return cursor < elements.size() - 2;
- } else {
- return cursor < elements.size() - 1;
- }
- }
- @Override
- public Integer next() {
- cursor += 2;
- if (cursor > elements.size() - 1) {
- cursor = 1;
- }
- return elements.get(cursor);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement