File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • 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
  • 0
  • 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: 47229
52
  • 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
  • 0
  • 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: 81
MySQL Database Netbeans IDE Ubuntu
  • 0
  • 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
  • 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: 81
MySQL Database Netbeans IDE Ubuntu
  • 0
  • 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: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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: 20372
44
Chrome Eclipse IDE Java Windows
  • 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: 47229
52
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic