• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Collections.synschronizedList

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can any one tell me how to modify the value of a synchronized list, given below is the snippet, when I tried it thrown UnsupportedOperation Exception



 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your code, and it worked without a problem. (after fixing syntax errors)
 
SaravanaKumar Venugopal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the prompt response Keith Lynn

Am sorry the code was like given below and it thrown UnsupportedOperationException and when I googled it has been said as when we convert to list by Arrays.asList() which will return fixed size of list and when we try to modify we will be getting UnsupportedOperationException. And there is no issue with the original post.

<code>
String s1 = "Value1,Value2,Value3,Value4";
List<String> lStr1 = Arrays.asList(s1.split(","));

lStr1.add("");
</code>

 
Marshal
Posts: 78659
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's have a look at the documentation for Collections#synchronizedList. It gives all sorts of dire warnings about what can go wrong if you don't synchronise the List correctly, but there is no suggestion of an unsupported operation Exception. You do get that Exception if you create the List with this method, however.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SaravanaKumar Venugopal wrote:And there is no issue with the original post.


Erm...yes there is:

  listOfString.add("value1")

is NOT a valid Java statement because it is missing a final ';'.

Could you also please read the page about using code tags, because our site does not use HTML tags.

Thanks.

Winston
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SaravanaKumar Venugopal wrote:Thanks for the prompt response Keith Lynn

Am sorry the code was like given below and it thrown UnsupportedOperationException and when I googled it has been said as when we convert to list by Arrays.asList() which will return fixed size of list and when we try to modify we will be getting UnsupportedOperationException. And there is no issue with the original post.

<code>
String s1 = "Value1,Value2,Value3,Value4";
List<String> lStr1 = Arrays.asList(s1.split(","));

lStr1.add("");
</code>



In your original post, you were converting a list to a synchronized list. When you do that, you can add to it.

In this example, you are taking an array and treating it as a list.

But since it is still an actual array, you cannot add to it.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic