This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
DW [ December 07, 2005: Message edited by: Jim Yingst ]
DW
There is always a bug :-)
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
There are a couple of nasties that you are observing here. First, arrays are fixed length, therefore, the List returned by Arrays.asList will throw an UnsupportedOperationException if a "resizing" operation is invoked (such as remove). That this behaviour is not clearly documented is a limitation of the API Specification.
Most importantly, that you can even invoke such an operation is a horrible flaw that is intrinsic to the List interface. In fact, I became so annoyed at having to put up with this and many other flaws of Java 2 Collections, that I set about writing them "more correctly" (within the confines of the broken language).
When you call Arrays.asList(), it will return a List object which is really only a wrapper around the array. The List will be read-only. You can't insert or remove items into that list.
To make it work, create a new ArrayList object (which is a real, modifiable implementation of List) to which you pass the List returned by Arrays.asList():
[Jesper]: When you call Arrays.asList(), it will return a List object which is really only a wrapper around the array. The List will be read-only.
It's not actually read-only. As Tony notes, you can't use operations which change the length, such as add() or remove(). But you can use set(). [ December 07, 2005: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Robin Sharma
Ranch Hand
Joined: Aug 24, 2005
Posts: 76
posted
0
Originally posted by Jesper de Jong: When you call Arrays.asList(), it will return a List object which is really only a wrapper around the array. The List will be read-only. You can't insert or remove items into that list.
To make it work, create a new ArrayList object (which is a real, modifiable implementation of List) to which you pass the List returned by Arrays.asList():