error with arrayList

B

Bumsys

I add file to arraylist
File file1 = new File("a.txt");
ArrayList arr = new ArrayList();
arr.add(file1);
then I want to get this file and do the following:
File filenew = (File)arr.get(0);
But I have error java.util.Arrays$ArrayList cannot be cast to
java.io.File.
Why?
 
D

Donkey Hot

(e-mail address removed) wrote in (e-mail address removed):
I add file to arraylist
File file1 = new File("a.txt");
ArrayList arr = new ArrayList();
arr.add(file1);
then I want to get this file and do the following:
File filenew = (File)arr.get(0);
But I have error java.util.Arrays$ArrayList cannot be cast to
java.io.File.
Why?

No idea, I tried it and it of course worked fine.

Are you sure you posted all the code? It looks like your arr.get(0) is not
a File, but an another ArrayList...
 
J

Jensie

I add file to arraylist
File file1 = new File("a.txt");
ArrayList arr = new ArrayList();
arr.add(file1);
then I want to get this file and do the following:
File filenew = (File)arr.get(0);
But I have error java.util.Arrays$ArrayList cannot be cast to
java.io.File.
Why?

The ClassCastException involves the (private) Arrays.ArrayList class,
which means you probably used Arrays.asList() somewhere.
Maybe you used ArrayList.add(Object) instead of
ArrayList.addAll(Collection).

If
File[] files = new File[] {new File("a.txt"), new
File("b.txt"), ...}

then

arr.add(Arrays.asList(files)) ==> adds Arrays.ArrayList to 'arr'
and raises ClassCastException in your code

arr.addAll(Arrays.asList(files)) ==> adds objects of type File
which is the intended behaviour

Define arr as List<File> to avoid this, the compiler will not allow
you to insert a collection (Arrays.ArrayList) into arr.
 
D

Donkey Hot

[...]
I'm with Donkey Hot, the syntax without pareentheses works just fine.
I think Jensie is on to the right answer.

Yeah, yeah...serves me right for making the idiotic assumption that
the code posted is the code that's actually not working. :)

Still, using ArrayList<File> would improve things. So I got that much
right. :)

Well yes, if the OP is using Java 5 or later. He did not mention. Generics
is great, and I'm waiting until my company gets it.
 
D

Donkey Hot

Is there a pre-Java 5, non-generic ArrayList class? I'd just assumed
that the only class named "ArrayList" was the actual generic
ArrayList<E> class. I don't see any non-generic version in the docs.

Sure there is. ArrayList works fine in 1.4.2
 
R

Roedy Green

I add file to arraylist
File file1 = new File("a.txt");
ArrayList arr = new ArrayList();
arr.add(file1);
then I want to get this file and do the following:
File filenew = (File)arr.get(0);
But I have error java.util.Arrays$ArrayList cannot be cast to
java.io.File.
Why?

The grown up way to handle this is to use generics. Then the casting
is automatic.

ArrayList<File> thefiles = new ArrayList<File>(20);
theFiles.add( new File( "a.txt" ) );

File got = theFiles.get(0);

If you do it the old way, you must you extra () to make it clear what
you are casting [ arr ] or [ arr.get() ].
 
A

Arne Vajhøj

Peter said:
Where did it go in the docs? I can't find the non-generic. Or is there
some aspect of the way classes are organized in the docs that I just
don't understand?

It did not go anywhere.

It is the same class.

In Java generics is a compile thing only - it is not in the
byte code.

Java pre-1.5:

ArrayList lst = new ArrayList();

is equivalent to Java 1.5+:

ArrayList<Object> lst = new ArrayList<Object>();

and the pre-1.5 syntax is still valid in 1.5+ (just
with a warning).

And:

ArrayList lst = new ArrayList();
ArrayList<Object> lst = new ArrayList<Object>();
ArrayList<Foobar> lst = new ArrayList<Foobar>();

all ends up as the same in the byte code.

Arne
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top