Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 11
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content
 
Marshal
Posts: 75634
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Use following methods:

unmodifiableSet -- for getting unmodifiable version of Set
unmodifiableMap
unmodifiableList

etc.
 
Ranch Hand
Posts: 83
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);
 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinayak jog wrote:



Better -- although the cast at line 3 is redundant.
 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75634
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I just had the craziest dream. This tiny ad was in it.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic