Cast checking against the erased type

Hi,

Perhaps someone can help with the following pesky problem:

I am receiving the following warning

Type Saftey: The cast from Object to ArrayList<Observer> is actually checking

against the erased type ArrayList.

Code snippit:

private Hashtable<String, ArrayList><Observer>> eventComponents = new Hashtable<String, ArrayList><Observer>>(10);

:

:

ArrayList<Observer> alComponents = eventComponents.get(subject);

:

:

The Line below generate the warning:

Type Saftey: The cast from Object to ArrayList<Observer> is actually checking

against the erased type ArrayList.

alComponents = (ArrayList<Observer>)alComponents.clone();

thanks

[788 byte] By [ahmad.haniffa] at [2007-11-26 17:33:38]
# 1

>

> alComponents =

> (ArrayList<Observer>)alComponents.clone();

>

The warning reminds you of the painful truth that Generic Types are not present at runtime: The VM will only execute a cast to the good old raw type ArrayList. As usual, this cast will fail if the object is not instanceof java.util.ArrayList.

But it will not be able to check whether this ArrayList ist really supposed to be an ArrayList<Observer>. You could also cast to ArrayList<Integer>, and the cast wouldn't fail. You could get rid of the warning by not using clone, but rather the copy constructor:

alComponents = new ArrayList<Observer>(alComponents);

McNeppa at 2007-7-9 0:01:39 > top of Java-index,Core,Core APIs...
# 2
Fantastic! That really helped :-)regardsSaj
ahmad.haniffa at 2007-7-9 0:01:39 > top of Java-index,Core,Core APIs...