• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Assigning arraylist variable to a method which returns of type AL

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot campbell
 
Campbell Ritchie
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic