• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to restrict the add in an ArrayList  RSS feed

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create an ArrayList with having some specific elements.
Now if any other class wants to access the list , it should only able to get the values but should be restricted for adding any new value into the list.

Tried to throw exception in the overriden add method but it did not give me the result .
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the possible ways:
You can use a Wrapper around the ArrayList- Create a class with ArrayList as a member- Make sure its private and there's no way to access this member from out side the Class. You can provide a method- addItem(T item) and then do the checking here to see if the value you are passing satisfies your requirement.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohammad , thanks for the suggestion but I do want to use the list outside the class as well . Only I want to restrict the add method.
So if I am getting your point ,private won't give me the desired result.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
buntha Choudhary wrote:Hi Mohammad , thanks for the suggestion but I do want to use the list outside the class as well . Only I want to restrict the add method.
So if I am getting your point ,private won't give me the desired result.


I meant- You make the List private, and provide a public add/get method where you can write your restrictions for adding the element to the List. You can retrieve the elements in the list via the getter method you provide.

Just a sample implementation. Something to get you started.

Note: Am using generics to make this class more general- to be used with what ever type you specify.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mohammad .
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could just extend ArrayList and override the add methods. That way you're specializing a class which is basically the whole OO principle. Also then you don't have to change (much) of your code (if you programmed against an interface):

 
Jason Cone
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use java.util.Collections.unmodifiableList(), which will return a read-only List that throws an UnsupportedOperationException if you try to modify its contents.

One thing to keep in mind, whether you create your own wrapper, extended class, or just use Collections.unmodifiableList(), is that while the returned/wrapped list will be read-only, other references to the List may still allow modification of its contents. For example:

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.
 
Joe carco
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Cone wrote:You could also use java.util.Collections.unmodifiableList(), which will return a read-only List that throws an UnsupportedOperationException if you try to modify its contents.

One thing to keep in mind, whether you create your own wrapper, extended class, or just use Collections.unmodifiableList(), is that while the returned/wrapped list will be read-only, other references to the List may still allow modification of its contents. For example:



Googles Guava project is also a very good alternative to Apache Commons. The library includes an implementation of Immutable collections like for eg. the ImmutableList or ImmutableMap etc
http://code.google.com/p/guava-libraries/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!