Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP 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: 21362
84
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
Pie
Posts: 49733
69
  • 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