1

I have these code:

class ReversibleArrayList<T> extends ArrayList<T> {
    public ReversibleArrayList(Collection<T> c) {
        super(c);
    }

    public Iterable<T> reversed() {
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                return new Iterator<T>() {
                    int current = size() - 1;

                    @Override
                    public boolean hasNext() {
                        return current >= 0;
                    }

                    @Override
                    public T next() {
                        return get(current--);
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
}

This is code is used to make ArrayList can be reversed in foreach.

There are two question:

1、I don't understand the method public Iterable<T> reversed(), the return value of this method is Iterable and there also has Iterator.

2、I think Interable just an interface, Does an interface can be used for return value?

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • 1
    this should come in handy for you http://stackoverflow.com/questions/6863182/what-is-the-difference-between-iterator-and-iterable-and-how-to-use-them – aurelius Nov 20 '15 at 14:33
  • Why would you extend an ArrayList?! Wouldn't you want `ReversedList` to be a decorator of `List`? – Claude Martin Nov 20 '15 at 14:37
  • @СӏаџԁеМаятіи I just want the `ArrayList` can be travel from end to front. – BlackMamba Nov 20 '15 at 14:42
  • Most would just create an interator like this: `list.listIterator(list.size());` Then use previous(): `while(list.hasPrevious()) { ... list.previous() ... }` – Claude Martin Nov 20 '15 at 14:45

2 Answers2

1

reversed() creates and return new Anonymous class (it means, it creates object of some unnamed class, that implements Iterable), that implements Iterable interface and implements method iterator() of that interface. And yes, interface can be used as return types, it is concept of OOP. It means that method can return everything that implements this interface.

saroff
  • 688
  • 9
  • 20
1

Sure an interface can be used as the return type. It just says, that the objects class implements the interface and thus has all of its methods available. The anonymous class fulfills these method requirements and can be used as the return type.

The code just looks complicated, because an anonymous class is used and the object is created and returned in quick succession.

Iterable:

"Implementing this interface allows an object to be the target of the "foreach" statement."

Source: http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html

Iterator:

"[...] an iterator generally points to a single instance in a collection. Iterable implies that one may obtain an iterator from an object to traverse over its elements - and there's no need to iterate over a single instance, which is what an iterator represents."

Source: PaulJWilliams answer Why is Java's Iterator not an Iterable?

Community
  • 1
  • 1
qwertz
  • 14,614
  • 10
  • 34
  • 46
  • The anonymous class is a recommend way to use in java\? These code can be modified for better understanding for example not using anonymous class? – BlackMamba Nov 20 '15 at 14:41
  • @BlackMamba I think it's ok in this instance, because the whole class only contains this bit of code (I assume), so it does not become that confusing. It's just a matter of readability: if anything gets too clumped up you better split the code into two classes. – qwertz Nov 20 '15 at 14:50