Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Cast Exception

Thread view: 
crazzybugger - 13 Sep 2006 17:03 GMT
ArrayList list=new ArrayList();
       list.add("Testing........");
       list.add("Jackie chan");
       Object []temp1=list.toArray();
       String [] temp2=(String [])temp1;

this piece of code throws Cast exception................ Since all the
list members are of type String why should it throw Cast Exception
unless it finds any of the members not to be a String ???
Arne Vajhøj - 13 Sep 2006 17:30 GMT
> ArrayList list=new ArrayList();
>         list.add("Testing........");
[quoted text clipped - 5 lines]
> list members are of type String why should it throw Cast Exception
> unless it finds any of the members not to be a String ???

You can't cast that way.

Java does not check all elements at that cast.

Use the toArray variant which takes an array as argument.

Arne
Manish Pandit - 13 Sep 2006 17:38 GMT
Casting an array does not mean that jvm will iterate through it and
cast every single array member. Here is what you can do to avoid
casting array types:

Object []temp1=  list.toArray();
String[] newArray = new String[temp1.length];
list.toArray(newArray);

the newArray will have what you need.

-cheers,
Manish

> ArrayList list=new ArrayList();
>         list.add("Testing........");
[quoted text clipped - 5 lines]
> list members are of type String why should it throw Cast Exception
> unless it finds any of the members not to be a String ???
Lord0 - 13 Sep 2006 18:27 GMT
Can anyone say "generics"?

Lord0
Manish Pandit - 13 Sep 2006 18:52 GMT
I dont think generics will help here. I assume by generics you mean
declaring the Arraylist as <String>, correct?

-cheers,
Manish

> Can anyone say "generics"?
>
> Lord0
Lord0 - 13 Sep 2006 19:15 GMT
> I dont think generics will help here. I assume by generics you mean
> declaring the Arraylist as <String>, correct?

Maybe my statement was a bit flippant. Yes, generics may not help here
but I'd still like to see them being used. After all Java 6 is due soon!
M.J. Dance - 14 Sep 2006 14:41 GMT
>> I dont think generics will help here. I assume by generics you mean
>> declaring the Arraylist as <String>, correct?
>
> Maybe my statement was a bit flippant. Yes, generics may not help here
> but I'd still like to see them being used. After all Java 6 is due soon!

Seems you have never been a part of a production environment. No half-serious
admin would replace a JVM the second a new version is out, no matter how super
duper it may appear to^H^H^H^H^H^H^H^H^Hbe. More often than not, production is
trailing by at least one version.

Plus, there are apps at least 10 years older than generics. Do you expect they
get a complete rewrite when some sintactic sugar comes out?
Tor Iver Wilhelmsen - 14 Sep 2006 15:52 GMT
> this piece of code throws Cast exception................ Since all the
> list members are of type String why should it throw Cast Exception
> unless it finds any of the members not to be a String ???

Because Object[] and String[] are distinct "subclasses" of Object
(internally called "[L/java/lang/Object;" and "[Ljava/lang/String;"
respectively), and you can only cast up or down one inheritance path.

The parameterless toArray() method you use creates a bona-fide
Object[].

You want to use the version of toArray() that takes an argument; the
type of that argument will be the type of the array. In fact, if the
size of the array is the same as the length of the list, the array
will be filled and returned, so you don't need to cast - just use the
array object afterewards.

ie.

String[] data = new String[list.size()];

list.toArray(data);

// data has been filled
Dobedani - 09 Oct 2006 17:22 GMT
Dear All,

The discussion is interesting, but for the moment I'm less interested
in performance. I would appreciate if someone can explain how to make
use of the toArray(T[] a) method. I have these classes:
1.
package trial;

public class Car {
   private String Id;
}

and 2.
package trial;
import java.util.ArrayList;

public class CarBusiness {
   private ArrayList<Car> cars;

   public Car[] getCars() {
       return cars.toArray(Car[] b ???);  // What does "T[] a" mean
???
   }
}

Instead of providing access by means of a method
public ArrayList<Car> getCars() { return cars; }

I prefer to only give "read-only" access. I guess this method can be
implemented by means of the toArray() method, but I don't understand
how exactly this should be done. Please help!

Kind regards,
Dobedani

Tor Iver Wilhelmsen schreef:

> > this piece of code throws Cast exception................ Since all the
> > list members are of type String why should it throw Cast Exception
[quoted text clipped - 20 lines]
>
> // data has been filled


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.