File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception while using arrays.aslist() method

 
Maheswaran Devaraj
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create a modifiable list from an array, you can do something like

List modifiable = new ArrayList(Arrays.asList(theArray));
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic