Thanks
jacov
It isn't a problem. You are able to cast a List to ArrayList, if that List
*is* an ArrayList (or a subclass).
I'm pretty sure the the List aMethod returns isn't an ArrayList if you get a
ClassCastException.
--
Kind regards,
Christophe Vanfleteren
Please don't toppost.
> THis is a very long standing problem in my programs, so i'm sure of
> this. But in don't understand why...
>
I'm sure it isn't. I'll repeat, if your method actually returns an
ArrayList, the cast *will* go fine. If it isn't, you'll get the
ClassCastException. It is that simple.
If you keep getting the exception, try printing the name of the class of the
instance that is returned from the method.
Also check this small test:
import java.util.*;
public class Test {
public List getList() {
return new ArrayList();
}
public List getLinkedList() {
return new LinkedList();
}
public static void main(String[] args) {
Test t = new Test();
ArrayList a = (ArrayList)t.getList();//will work
ArrayList c = (ArrayList)t.getLinkedList();//will not work
For the most part, it's that simple. However, here's some things to
watch out for:
1. Two classes can have the same name, but be different because they are
in different packages, or one is an inner class of another, or some such
thing. If you've written your own class called ArrayList, then there
may be some confusion as to which ArrayList you're working with.
There's also a (non-public) API class called java.util.Arrays$ArrayList
-- IIRC -- which is quite different from the standard
java.util.ArrayList class. So the class being called ArrayList isn't
good enough; it has to actually be the same class.
2. If there are multiple classloaders involved (whether you used them
explicitly or you're working in an environment such as a servlet
container or J2EE application server that uses them implicitly) then
there are a few more ways this can go wrong even for classes with the
same fully qualified name.
3. It's possible to forget that doing something like
Collections.synchronizedList or Collections.unmodifiableList will
actually return a List of a different implementation class than the
original list that was passed into the method. You could catch this one
with a simple print of getClass().getName(), as Christophe suggested.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
> 1. Two classes can have the same name, but be different because they are
> in different packages, or one is an inner class of another, or some such
> thing. If you've written your own class called ArrayList, then there
> may be some confusion as to which ArrayList you're working with.
> There's also a (non-public) API class called java.util.Arrays$ArrayList
> -- IIRC -- which is quite different from the standard
> java.util.ArrayList class. So the class being called ArrayList isn't
> good enough; it has to actually be the same class.
Don't forget java.awt.List as well. This one has caused me grief when i
trusted a little too much on Eclipse quickfixes. If you say something like
import java.awt.List;
public class Foo {
void bar () {
List l = (List) new ArrayList();
}
}
you will rightfully get a CCE