Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Arrays.asList() returning java.util.Arrays$ArrayList

Reply
Thread Tools

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

 
 
Alexandra Stehman
Guest
Posts: n/a
 
      06-17-2004
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=%2...mon.com&rnum=1
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-17-2004
Alexandra Stehman <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"There's no business like slow business."
- Tailgunner
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-17-2004
On 17 Jun 2004 10:12:09 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
Bent C Dalager
Guest
Posts: n/a
 
      06-17-2004
In article <(E-Mail Removed) >,
Alexandra Stehman <(E-Mail Removed)> 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 - (E-Mail Removed) - http://www.pvv.org/~bcd
powered by emacs
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-17-2004
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
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-17-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
cannot access Internet after returning from standby =?Utf-8?B?REJI?= Wireless Networking 0 10-31-2005 09:21 PM
Returning multiple variables Kwaj VHDL 2 06-07-2004 10:20 AM
WebService – Returning an Object Karuppasamy ASP .Net 0 01-15-2004 04:38 PM
Re: Net::SNMP - returning huge hex for opaque value dna Perl 0 10-20-2003 02:41 PM



Advertisments