| Author |
How to handle UnsupportedOperationException while using lists
|
Vinny Bhat
Greenhorn
Joined: Apr 30, 2012
Posts: 10
|
|
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
|
Vinny
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2927
|
|
|
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
|
Mohamed Sanaulla | My Blog
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4750
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Vinny Bhat
Greenhorn
Joined: Apr 30, 2012
Posts: 10
|
|
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
Joined: Apr 30, 2012
Posts: 10
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Vinny Bhat
Greenhorn
Joined: Apr 30, 2012
Posts: 10
|
|
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
Joined: Mar 17, 2011
Posts: 4750
|
|
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
Joined: Apr 30, 2012
Posts: 10
|
|
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
Joined: Mar 17, 2011
Posts: 4750
|
|
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
Joined: Apr 30, 2012
Posts: 10
|
|
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!
|
 |
 |
|
|
subject: How to handle UnsupportedOperationException while using lists
|
|
|