Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format ." Watch "UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format ." New topic
Author

UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format .

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212


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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
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

Joined: Oct 13, 2005
Posts: 39044
    
  23
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.
 
GeeCON Prague 2014
 
subject: UnsuppourtedOperationException when tried to modify the Collections.unmodifiable format .