File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to restrict the add in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to restrict the add in an ArrayList" Watch "How to restrict the add in an ArrayList" New topic
Author

How to restrict the add in an ArrayList

buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

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

Joined: Sep 08, 2007
Posts: 2928
    
  15

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.


Mohamed Sanaulla | My Blog
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

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

Joined: Sep 08, 2007
Posts: 2928
    
  15

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

Joined: Jul 03, 2009
Posts: 136

Thanks Mohammad .
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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):



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jason Cone
Greenhorn

Joined: Jul 21, 2004
Posts: 25
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

Joined: Jul 03, 2009
Posts: 136

Thanks to all.
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
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/
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to restrict the add in an ArrayList
 
Similar Threads
How List interface work ?
Question on Generics
Generics + Polymorphism
Q about generic compiler warnings
Generics...please clarify