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

A

Alexandra Stehman

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="[email protected]&rnum=1
 
J

Joona I Palaste

Alexandra Stehman said:
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.
 
R

Roedy Green

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.
 
B

Bent C Dalager

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
 
C

Chris Smith

Roedy said:
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
 
C

Chris Smith

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

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top