Cast Exception

C

crazzybugger

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 ???
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

crazzybugger said:
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 ???

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
 
M

Manish Pandit

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
 
M

Manish Pandit

I dont think generics will help here. I assume by generics you mean
declaring the Arraylist as <String>, correct?

-cheers,
Manish
 
L

Lord0

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

M.J. Dance

Lord0 said:
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?
 
T

Tor Iver Wilhelmsen

crazzybugger said:
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
 
D

Dobedani

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:
crazzybugger said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top