error with arrayList

Discussion in 'Java' started by Bumsys@gmail.com, May 23, 2008.

  1. Guest

    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?
    , May 23, 2008
    #1
    1. Advertising

  2. Donkey Hot Guest

    wrote in news:45f0a0b3-d4d1-483a-86ae-
    :

    > 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...
    Donkey Hot, May 23, 2008
    #2
    1. Advertising

  3. Jensie Guest

    On May 23, 8:28 am, wrote:
    > 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.
    Jensie, May 23, 2008
    #3
  4. Donkey Hot Guest

    "Peter Duniho" <> wrote in
    news:eek::

    > On Fri, 23 May 2008 06:04:41 -0700, Lew <>
    > wrote:
    >
    >> [...]
    >> 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.
    Donkey Hot, May 23, 2008
    #4
  5. Donkey Hot Guest

    "Peter Duniho" <> wrote in
    news:eek::

    > On Fri, 23 May 2008 11:49:06 -0700, Donkey Hot
    > <-a-geek.com> wrote:
    >
    >>> 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.

    >
    > 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
    Donkey Hot, May 23, 2008
    #5
  6. Roedy Green Guest

    On Thu, 22 May 2008 23:28:11 -0700 (PDT), wrote,
    quoted or indirectly quoted someone who said :

    >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() ].
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, May 23, 2008
    #6
  7. Peter Duniho wrote:
    > On Fri, 23 May 2008 12:56:52 -0700, Donkey Hot <-a-geek.com>
    > wrote:
    >> Sure there is. ArrayList works fine in 1.4.2

    >
    > 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
    Arne Vajhøj, May 24, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,730
    Natty Gur
    Aug 19, 2003
  2. VB Programmer

    Problem: Late binding error with arraylist

    VB Programmer, Jun 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    413
    John Saunders
    Jun 18, 2004
  3. Kaidi
    Replies:
    4
    Views:
    2,337
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,346
  5. Philipp
    Replies:
    6
    Views:
    906
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page