This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format .

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I read that we would get an UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format .

But why i am not getting any exception .The console is blank
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why i am not getting any exception .The console is blank


The Collections.unmodifiableList() method returns a read-only list. It doesn't change the list that is passed to it.

Henry
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried as you said .and i got my Exception

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
at Tes.main(Tes.java:19)


Thank you.
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read the documentation for List#add() which explains it. Also the Collections#unmodifiableList(java.util.List) method.

The return value is specifically designed not to allow additions or removals from the List. So it throws Exceptions if you try to use the unimplemented methods.
If the original List changes, the "unmodifiable" copy changes to match, however.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic