File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exception while using arrays.aslist() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception while using arrays.aslist() method" Watch "Exception while using arrays.aslist() method" New topic
Author

Exception while using arrays.aslist() method

Maheswaran Devaraj
Ranch Hand

Joined: Sep 14, 2007
Posts: 47
Hi All,

I got a list from the Arrays.asList() method() while i try to remove a element from it the compiler spiting the unsupported exception.Can any one explain the code.




Thanks and Regards,
Mahes
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Arrays.asList returns an unmodifiable List view of the array. You can't add to or remove from this list.

Change your list code to this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

To create a modifiable list from an array, you can do something like

List modifiable = new ArrayList(Arrays.asList(theArray));


[Jess in Action][AskingGoodQuestions]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2995
    
    9
Rob Prime wrote:Arrays.asList returns an unmodifiable List view of the array. You can't add to or remove from this list.

I think calling it unmodifiable is misleading, since you can set() an element. You just can't do anything that would change the length, like add() or remove(). Just like an array.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

You're right, I forgot about setting new values.

So let's phrase it like this: with the List returned by Arrays.asList, you can't perform an operation that is not supported by the backing array. Arrays allow setting, but not changing the size in any way, and neither does this List.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception while using arrays.aslist() method
 
Similar Threads
converting Arrays to List?
Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays
Conversion of Arrays to List - code causing compiler error
Understanding List and String.
Need help on generics