aspose file tools*
The moose likes Beginning Java and the fly likes Assigning  arraylist variable to a method which returns of type  AL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning  arraylist variable to a method which returns of type  AL" Watch "Assigning  arraylist variable to a method which returns of type  AL" New topic
Author

Assigning arraylist variable to a method which returns of type AL

Prem Bhazee
Ranch Hand

Joined: Feb 18, 2006
Posts: 34
Hi Ranchers,
I have an arraylist question.



In my method methodxyz() when I call appendDetailstoAlList which returns an arraylist how should I declare the arraylist variable. Should I just create a reference of an arraylist variable and assign to the method or should I instantiate a arraylist and then refer to the variable of type arraylist. Please advise.
Thanks
Bhazee
[ April 28, 2008: Message edited by: Prem Bhazee ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Difficult to understand what you require, but I don't like your solution.

Do you really want the ArrayList to be a local variable to methodxyz? It looks as thought it ought to be a field of the class, in which case it ought to be initialised in the constructor.
Then you can give the appendDetails method a void return type, and use it to add the details to the List.

If you want people to have access to the List of lines from the File, use the Collections#unmodifiableList method in a getList() method to return a List which is in read-only format; then other classes can't alter the contents of the List.

And don't declare an ArrayList as ArrayList; declare it as List.

List<XYZ> myList;
. . .
myList = new ArrayList<XYZ>();

That way you can alter the implementation and (as long as you don't use ArrayList-specific methods like ensureCapacity) it will still work.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

Well, first of all it is good to use generics to arraylist with and above java 1.5 .

When you create an arraylist using this statement

A new object of arraylist with a reference is created with a default size of 10. And when you try to assign the return value of the method appendFileDetailstoAlListto the arraylist the previously created arraylist object is unreferenced.


when in the second case , just a reference is created and we can assign the return value of the method.
[ April 28, 2008: Message edited by: Ravikanth kolli ]

-kolli
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

hye Campbell Ritchie,

I am not sure if the question is asked before, but what is the difference between declaring the arraylist as arraylist and list?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
This point comes up at least once a week. It is called "program to the interface."

There is a List interface, which you can find in the Java™ API; it contains the names of all the methods in Lists. If you say you want a List, that is what you get. You can later say you want a particular implementation, and in the standard API you get these implementations:

AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector

If you declare myList to be of type "List" you can change the implementation to anything which implements it (maybe not those called abstract) confident that your application will still work. You can swap an ArrayList for a LinkedList, for example.

If you do a search on JavaRanch for "program to the interface" you get several other discussions: two recent examples are here and a totally useless example, since I was involved :wink: . There is bound to be lots more available if you Google for "program to the interface."
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

Thanks a lot campbell
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're welcome
 
wood burning stoves
 
subject: Assigning arraylist variable to a method which returns of type AL