Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public Iterator<T> iterator() {
- return new IteratorImpl(t -> t!=null && ((Automobile)t).getPrice() == 20.);
- }
- private class IteratorImpl implements Iterator<T> {
- boolean condition = false;
- Predicate<T> p;
- int cursor;
- IteratorImpl() {
- p = t -> true;
- }
- IteratorImpl(Predicate<T> pred) {
- p = pred;
- }
- @Override
- public boolean hasNext() {
- while (cursor != size) {
- if(p.test(automobileList[cursor])) {
- return true;
- }
- cursor++;
- }
- return false;
- }
- @Override
- public T next() {
- T auto;
- while (hasNext()) {
- if(p.test(auto = automobileList[cursor++])) {
- condition = true;
- return auto;
- }
- }
- throw new NoSuchElementException();
- }
- @Override
- public void remove() {
- if (!condition) {
- throw new IllegalStateException();
- }
- condition = false;
- if (cursor != size) {
- System.arraycopy(automobileList, cursor, automobileList, cursor - 1, size - cursor);
- }
- cursor--;
- size--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement