Typecasting arraylist

I have a class that extends a generic type Arraylist:

class ListA extends ArrayList<A>{

}

Now I create an object ListAand then I want to make it a synchronized list

ListA a = new ListA();
a = (ListA) Collections.synchronizedList(a);

But the above code gives a type exception. The last thing I want to aiterate over the object and save the memebers list in another synchronized list.

Any suggestions on how to do this?

+3
source share
2 answers

Replace the class that inherits from ArrayList<A>with the interface and the class containing the list:

interface ListA extends List<A> {
    // Put additional methods here
}
class ListAImpl implements ListA {
    private List<A> content;
    public ListAImpl(List<A> content) {
        this.content = content;
    }
    // Use delegation for all methods of the List<A> interface, calling through
    // to the content list.
}

ListA , : "" ArrayList<A>, ListA, , ListA.

, ListA , :

ListA a = new ListAImpl(new ArrayList<A>());
ListA sync = new ListAImpl(Collections.synchronizedList(a));
+2

Collections.synchronizedList()

public static <T> List<T> synchronizedList(List<T> list) {

, a List. . , , ListA.

, .

+2

All Articles