Class cast exception

From: Will (
Date: 10/18/04

Date: 18 Oct 2004 11:51:51 -0700

I get a Class Cast Exception at runtime with the following code, and
was wondering why this isn't allowed:

Vector v = new Vector();
//do some stuff to fill in v with a bunch of Strings
String [] s = (String [])(v.toArray());

The toArray method returns an Object [] so why can't I cast it to a
String [] if I know that everything in it will be a String? The only
workaround I've found is to make a second array and manually copy and
cast each element of the first array into the second array.

Relevant Pages

  • Re: Invalid CastException with SqlDBType
    ... Add(string*, SqlDbType, int, string**) ... The DateTime.Now is trying to cast into a int. ... not in Sql set up the column as identity and let it seed itself. ... > update a MS SQL Database with a stored procedure. ...
  • Re: Porting up to VC++ 2005
    ... Since you are dealing with strings that contain file names, ... characters, but that's a different question to be handled in a different way. ... done, which should be unnecessary, it should be an LPTSTR cast, which ... It only applies to string and ...
  • Re: Confused about using Reflection to examine a collection
    ... the precise type that Reflection is saying it is. ... say that 'obj' is actually a custom collection. ... If it were a basic type, such as 'string' then I would do a cast like this: ...
  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... the boolean is implicitly cast to a string. ... If you look up the manual on type casting, you'll find that FALSE is cast to an empty string, and TRUE is cast to a non-empty string. ...
  • Re: Error whilst processing ORDER BY clause
    ... My guess would be that you need to CAST it to a boolean since it is assuming ... > We receieve the error "Error whilst processing ORDER BY clause in the SQL ... name in the clause to reference a string or int data type it also succeeds, ... Can we use a CAST ...