Re: checking casts



Dan Upton wrote:
When trying to compile something I was working on today, I got this error based on a performing a cast:

PuzzleSolver.java:311: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.Vector<Piece>
                            piecesClone = (Vector<Piece>)pieces.clone();

I know it's a safe cast, but just for the sake of getting javac to leave me alone I tried sticking it in a try...catch block:

The compiler is complaining that that you are casting to Vector<Piece>, but the runtime will not be able to check that the object isn't actually, say Vector<String>.


The easy way around it is to write:

        piecesClone = new ArrayList<Piece>(pieces);

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages