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 ]
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.
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 ]
I am not sure if the question is asked before, but what is the difference between declaring the arraylist as arraylist and list?
Joined: Oct 13, 2005
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:
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.