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.
The moose likes Java in General and the fly likes How to use remove() method on an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to use remove() method on an ArrayList" Watch "How to use remove() method on an ArrayList" New topic
Author

How to use remove() method on an ArrayList

Robin Sharma
Ranch Hand

Joined: Aug 24, 2005
Posts: 76
Hi!

I get the UnsupportedOperationException exception when i try to run this code:


Please help.

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

For your case, I'd use:
http://www.contractualj.com/api/net/tmorris/adt/sequence/ArraySequence.html


Tony Morris
Java Q&A (FAQ, Trivia)
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12928
    
    3

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():


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[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
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():


Thanks Jesper. Your solution is working fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use remove() method on an ArrayList
 
Similar Threads
Gradebook
Struts 2 checkboxlist error - list key could not be resolved
dealing with compiler warnings
Equals Doubt
equals override: 2