• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to handle UnsupportedOperationException while using lists  RSS feed

 
Vinny Bhat
Greenhorn
Posts: 10
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a String array in my code which I will be converting to a list using Arrays.asList at some point of time.

After convertion, I will not be able to add to it or remove any element from it. When I do this, I get UnsupportedOperationException.

Whereas when I create a new list using java.util.List I do not face such problem.

Does Arrays.asList not return a util.List object? Or can't we do any operations on such a lsit created by Arrays.asList?

Any help would be greatly appreciated.
Thanks in advance!

Vinny
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays.asList() returns a Read only List and hence you will not be able to modify its contents. A related thread here which discusses this in detail
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinny Bhat wrote:Does Arrays.asList not return a util.List object? Or can't we do any operations on such a lsit created by Arrays.asList?

The latter; and if you think about it, its quite correct. You can't dynamically extend an array, so it would be kind of silly to allow you to add to a List that's based on an array.

If you want to create a List that you can add to, you could do something like:
List<X> fromArray = new ArrayList<X>( Arrays.asList(myArray) );

Winston
 
Vinny Bhat
Greenhorn
Posts: 10
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Vinny Bhat wrote:Does Arrays.asList not return a util.List object? Or can't we do any operations on such a lsit created by Arrays.asList?

The latter; and if you think about it, its quite correct. You can't dynamically extend an array, so it would be kind of silly to allow you to add to a List that's based on an array.

If you want to create a List that you can add to, you could do something like:
List<X> fromArray = new ArrayList<X>( Arrays.asList(myArray) );

Winston


Thanks a lot Winston, that really makes sense. Actually the requirement was such that at the persistence layer, the arrays are used and at the view layer it is changed to list and along with the contents of the array, we have to add some more elements to the list. That was the reason I was trying to add to the list once it is converted from array.
 
Vinny Bhat
Greenhorn
Posts: 10
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Arrays.asList() returns a Read only List and hence you will not be able to modify its contents. A related thread here which discusses this in detail


Thanks Mohamed for the other post mentioned,that really helped.

Vinny
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Arrays.asList() returns a Read only List and hence you will not be able to modify its contents.

Not completely true. It's almost immutable. You can't add or remove elements, but you can still replace elements.
 
Vinny Bhat
Greenhorn
Posts: 10
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Mohamed Sanaulla wrote:Arrays.asList() returns a Read only List and hence you will not be able to modify its contents.

Not completely true. It's almost immutable. You can't add or remove elements, but you can still replace elements.


Rob,

I am new to these concepts. Can you please elaborate with an example? Thanks for your time
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinny Bhat wrote:
Rob Spoor wrote:
Mohamed Sanaulla wrote:Arrays.asList() returns a Read only List and hence you will not be able to modify its contents.

Not completely true. It's almost immutable. You can't add or remove elements, but you can still replace elements.

I am new to these concepts. Can you please elaborate with an example? Thanks for your time

Arrays.asList(myArray).set(0, someNewValue);

Winston
 
Vinny Bhat
Greenhorn
Posts: 10
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Vinny Bhat wrote:
Rob Spoor wrote:
Mohamed Sanaulla wrote:Arrays.asList() returns a Read only List and hence you will not be able to modify its contents.

Not completely true. It's almost immutable. You can't add or remove elements, but you can still replace elements.

I am new to these concepts. Can you please elaborate with an example? Thanks for your time

Arrays.asList(myArray).set(0, someNewValue);

Winston


Thanks Winston!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinny Bhat wrote:Thanks Winston!

You're welcome. It should be noted that the above could throw an Exception (if myArray was empty).

Winston
 
Vinny Bhat
Greenhorn
Posts: 10
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
You're welcome. It should be noted that the above could throw an Exception (if myArray was empty).

Winston


Ya I noticed this during the implementation when I tried this fro my sample code. Thanks again!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!