wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content" Watch "How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content" New topic
Author

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

Anjali Lamba
Greenhorn

Joined: Feb 02, 2011
Posts: 11

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

Joined: Oct 13, 2005
Posts: 37907
    
  22
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

Joined: Oct 15, 2010
Posts: 281


Use following methods:

unmodifiableSet -- for getting unmodifiable version of Set
unmodifiableMap
unmodifiableList

etc.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81

ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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

Joined: Apr 01, 2011
Posts: 81

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

vinayak jog wrote:



Better -- although the cast at line 3 is redundant.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content
 
Similar Threads
Collections Framework - use of Vector
writing to files
file as temporary data storage
Differences beteween ArrayList and Vector.
2-d arrays