Arrays.asList() returning java.util.Arrays$ArrayList

Discussion in 'Java' started by Alexandra Stehman, Jun 17, 2004.

  1. Does anyone know a good way to get the ArrayList out of the
    java.util.Arrays$ArrayList returned by Arrays.asList()?

    It is rather inconvenient to have java.util.Arrays$ArrayList returned
    from Arrays.asList(). Here I was, scratching my head at where in the
    world a CCE was coming from. Surprise! You don't have an ArrayList,
    you have an Arrays$ArrayList. Back to Object[]s.

    Related post:
    http://groups.google.com/groups?q="&rnum=1
    Alexandra Stehman, Jun 17, 2004
    #1
    1. Advertising

  2. Alexandra Stehman <> scribbled the following:
    > Does anyone know a good way to get the ArrayList out of the
    > java.util.Arrays$ArrayList returned by Arrays.asList()?


    It implements List, doesn't it? Create a new ArrayList and call its
    addAll() method with your Arrays$ArrayList.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "There's no business like slow business."
    - Tailgunner
    Joona I Palaste, Jun 17, 2004
    #2
    1. Advertising

  3. Alexandra Stehman

    Roedy Green Guest

    On 17 Jun 2004 10:12:09 -0700, (Alexandra Stehman)
    wrote or quoted :

    >Does anyone know a good way to get the ArrayList out of the
    >java.util.Arrays$ArrayList returned by Arrays.asList()?


    Is the list you get out read-only? In what ways does it differ from
    an ArrayList. I'd like to know why they did that.

    You could fairly easily write your own static asArrayList method. It
    would not be as general, unable to handle int[] arrays.

    You'd think that for Object[] they would have returned an ArrayList.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 17, 2004
    #3
  4. In article <>,
    Alexandra Stehman <> wrote:
    >Does anyone know a good way to get the ArrayList out of the
    >java.util.Arrays$ArrayList returned by Arrays.asList()?


    Object[] array = ...;
    ArrayList alist = new ArrayList(Arrays.asList(array));

    Cheers
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
    Bent C Dalager, Jun 17, 2004
    #4
  5. Alexandra Stehman

    Chris Smith Guest

    Roedy Green wrote:
    > Is the list you get out read-only? In what ways does it differ from
    > an ArrayList. I'd like to know why they did that.


    It's done because there are different operations available on an array
    versus an ArrayList. The List implementation that wraps an array, for
    example, can't implement the add or remove methods, since there's simply
    no corresponding operation on arrays.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 17, 2004
    #5
  6. Alexandra Stehman

    Chris Smith Guest

    Alexandra Stehman wrote:
    > Does anyone know a good way to get the ArrayList out of the
    > java.util.Arrays$ArrayList returned by Arrays.asList()?


    There is *not* an ArrayList (in the sense you're thinking of it) in
    there anywhere. That is a completely separate class that just happens
    to be *named* ArrayList. So you can't extract any existing ArrayList
    from it. That's only natural; there are operations you can do to an
    ArrayList (add, remove, etc.) that couldn't possibly be done to an
    array.

    As Joona pointed out, you can easily copy the contents into an
    ArrayList, as follows:

    ArrayList list = new ArrayList(Arrays.asList(myArray));

    In that case, since you've made a copy into a data structure capable of
    all the ArrayList operations, they are all available.

    > It is rather inconvenient to have java.util.Arrays$ArrayList returned
    > from Arrays.asList().


    No, it's really not. Why do you need an ArrayList? Why can't you just
    use a List (which does implement RandomAccess, if that's important)?
    The only things you can't do with a List are clone(), trimToSize(), and
    ensureCapacity(). The last two methods are irrelevant for a List built
    from an array, since it's always a fixed size. Cloning is a bit of an
    issue, I suppose, but it's always a problem with Collections; just use a
    constructor to copy the List instead. This specific List doesn't
    support operations that add or remove elements either, but that's only
    natural given what it is. If you wanted a copy of the array that you
    can change, then the one line of code above gives you that.

    > Here I was, scratching my head at where in the
    > world a CCE was coming from. Surprise! You don't have an ArrayList,
    > you have an Arrays$ArrayList. Back to Object[]s.


    Object[]? That's certainly no ArrayList either, and provides even less
    functionality. You seem to be missing something fundamental here. If
    you had poorly written APIs that require the use of an ArrayList, that
    would be one thing... but that doesn't seem to be the case. Why do you
    think it's a problem that the result of Arrays.asList is not an
    ArrayList?

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 17, 2004
    #6
    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. Adam Monsen
    Replies:
    3
    Views:
    657
    Adam Monsen
    May 20, 2007
  2. Ken
    Replies:
    35
    Views:
    2,246
  3. laredotornado
    Replies:
    19
    Views:
    1,725
    Arne Vajhøj
    Nov 9, 2009
  4. Alex J
    Replies:
    4
    Views:
    680
    Arne Vajhøj
    Jul 22, 2011
  5. Roedy Green
    Replies:
    2
    Views:
    127
    Stanimir Stamenkov
    May 10, 2014
Loading...

Share This Page