This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content

 
Anjali Lamba
Greenhorn
Posts: 11
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the java.util.Collections class, where you will find methods allowing you to make a read-only copy of Lists, etc.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Use following methods:

unmodifiableSet -- for getting unmodifiable version of Set
unmodifiableMap
unmodifiableList

etc.
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);
 
Harsha Smith
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak jog wrote: ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);


wrong answer. throws class cast exception

 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:
vinayak jog wrote: ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);


wrong answer. throws class cast exception




List<String> al = new ArrayList<String>();
al.add("hello world");

al =(List)Collections.unmodifiableList(al);
System.out.println(al.get(0));
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak jog wrote:



Better -- although the cast at line 3 is redundant.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go through the java.util.Collections class, where you will find methods allowing you to make a read-only copy of Lists, etc.

Not a copy - a view. A copy implies that the data is copied and that modifying the original will not affect the copy. A view implies that the data is not copied and changes in the original will be seen in the view.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, Rob. One needs to be precise.
Note that the unmodifiable method returns a view, but the original List is still usually extant, and that can be modified.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic