aspose file tools*
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
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: 40061
    
  28
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: 294


Use following methods:

unmodifiableSet -- for getting unmodifiable version of Set
unmodifiableMap
unmodifiableList

etc.


SQL Query Interview Question
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: 24187
    
  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: 19792
    
  20

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: 40061
    
  28
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