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

unmodifiableList(List list)

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using Linkedlist in my program and i want to make it unmodifiable thru out my program so i am using this method


LinkedList<Object> l=new LinkedList<Objectl.add("a");
l.add("b");
l.add("c");
l.add("d");
List list=Collections.unmodifiableList(l);//here is an error

while i use arraylist,vector in place of linkedlist it is not showing any error

why?
linkedlist is also implementing the List interface as arraylist and vector does


:roll:


please somebody give me the best suggestion with example( iam learning OOPs through collection frame work)
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the exception that you are getting. The following program works using LinkedList:

 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run your code and there are no errors in it.

What errors do you receive? Compile time? Run time?

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are likely receiving a compile-time warning.
List list=Collections.unmodifiableList(l);//here is an error
Try this:
List<Object> list=Collections.unmodifiableList(l);//here is an error

As a side note, I strongly recommend against using collections in this way (or even at all).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic