Results 1 to 10 of 10
Thread: Creating ArrayList
- 08-07-2010, 08:56 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Creating ArrayList
Hey there.
I am stuck with creating arraylists.
I have seen them created with reference to List:
List myNewList = new ArrayList();
Also with a reference to ArrayList:
ArrayList myNewList = new ArrayList();
My question is:
Why would you declare an ArrayList with a reference to List?
Are there any advantages over the second method: "ArrayList myNewList = new ArrayList();" ???
I am completely new to java, so this question may seem pretty dumb.
But thanks anyways :D
- 08-07-2010, 10:20 AM #2
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
I don't have the understanding to give you a good answer, but I can tell you that if you read about inheritance and polymorphism you will get your answer.
- 08-07-2010, 01:29 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Suppose your method(s) are only interested in the functionality of a List (the interface) and don't care about the peculiarities of ArrayLists or LinkedLists or whatever (the implementations of the List interface); all they care about is they're dealing with a List. So why not treat a particular implementation as a List? You can do something like this:
... and deal with the list object as if it were just a List<T> (as a matter of fact is is a List<T> object). If you want to, or have to use another type of List<T> in the future, all you have to do is change one line, the line above. As a rule of thumb: consider and object of class C as the uppermost superclass B thereof, i.e. B is an ancestor class (or interface) of C.Java Code:List<T> list= new ArrayList<T>();
kind regards,
Jos
- 08-07-2010, 06:24 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
thanks allot
I think I understand.
Just to clarify:
(1) When writing: List<T> list= new ArrayList<T>();
Is it true to say:
Data type is: of type "List interface"?
Does this mean it's actually an ArrayList object but only the methods declared in the List interface are visible in the object?
(2) In what situation would it be an advantage to use:
(i) ArrayList<T> list= new ArrayList<T>();
rather than
(ii) List<T> list= new ArrayList<T>()
??
I know you said to use (ii) if your methods don't care about the extra stuff that (i) gives the object.
But why can't I use: "ArrayList<T> list= new ArrayList<T>();" all the time and just use the stuff I need from it?
Sorry if my questions are confusing
I aint sure what I'm talking about
thanks allot.
- 08-07-2010, 07:31 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Yes, as you define the type of your variable as List<T> the type of the variable is 'interface List<T>'. You can define the type as 'ArrayList<T>' if you want (not much wrong with that) but as soon as you have to change the implementation type, e.g. to 'LinkedList<T>' you have to change it everywhere in your code and recompile everything again. For a small project it doesn't matter much, but what if your code is installed everywhere around the world? Your customers won't appreciate a new version again just because you have changed a bit of it.
kind regards,
Jos
- 08-08-2010, 07:26 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
So you can definitely define a variable of type interface?
Can you name a situation where you would have to do that?change the implementation type, e.g. to 'LinkedList<T>'
Won't you just have to change it in the declaration of the variable. (So only once) ?you have to change it everywhere in your code and recompile everything again
Thanks again for your time.
- 08-08-2010, 08:12 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Yes, that's what I was saying all the time.
When you have a better implementation available for the job you can easily change to that one without changing one other line of code.
But then again, the variable has to be known by the interface type name, not it's implementing class name.
kind regards,
Jos
- 08-08-2010, 08:31 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
ok, I think I finally get it
Thanks a bunch!
- 08-08-2010, 04:14 PM #9
JosAH. Are you talking about something like this?
Java Code:List<String> test = new ArrayList<String>(); ... test = new LinkedList<String>();
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 08-08-2010, 04:16 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
ArrayList
By rusBoy in forum Advanced JavaReplies: 1Last Post: 11-12-2009, 11:36 AM -
Creating files stopped creating...
By Dieter in forum Advanced JavaReplies: 3Last Post: 09-25-2009, 11:45 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Creating an ArrayList from an existing LinkedList
By Java Tip in forum Java TipReplies: 0Last Post: 12-05-2007, 02:09 PM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks