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