Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception while using arrays.aslist() method

 
Maheswaran Devaraj
Ranch Hand
Posts: 47
  • 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: 20529
54
Chrome Eclipse IDE Java Windows
  • 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: 24211
35
Chrome Eclipse IDE Mac OS X
  • 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: 3076
14
  • 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: 20529
54
Chrome Eclipse IDE Java Windows
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic