• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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: 2410
  • 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: 73767
332
  • 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: 2410
  • 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.
 
eat bricks! HA! And here's another one! And a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic