when i try to cast a reference to an object to an arraylist of strings, i get a warning of unchecked casting, how do I remove it?
I used something like (ArrayList<String>)obj;
Printable View
when i try to cast a reference to an object to an arraylist of strings, i get a warning of unchecked casting, how do I remove it?
I used something like (ArrayList<String>)obj;
Instead of "I used something like" why don't you post what you actually used? A SSCCE will provide us with much more information about your problem.
You can't remove that warning; generics is a compile time only thing, i.e. during runtime only ArrayLists exist. If you take away the type parameter:
... the compiler will whine about your program using 'raw types'. You won't get a warning when you apply an ordinary down cast, as in:Code:ArrayList al= (ArrayList)obj;
Here the down cast will be applied at runtime and if the cast fails an Exception will be thrown. Generic types always cause a warning here and the compiler assumes that you know what you're doing but it whines a little about it to make sure.Code:Double d= (Double)obj;
kind regards,
Jos
What about arrays, can I down cast an Object into a String array [] without having the compiler whine or warn?
Good; muffling away warnings fires back one day. The compiler doesn't issue a warning for no reason, no matter how futile it may seem. One day the unsafe situation you have created might stab you in the back while the compiler had warned you about it. If you really know (or you think you know) what you're doing silencing individual warnings can be forgiven but always be very careful.
kind regards,
Jos