| Author |
Arraylist and linklist !!
|
Saurabh Agrawal
Ranch Hand
Joined: Oct 07, 2003
Posts: 238
|
|
Hello friends , I am back here with some doubts of mine and hope that you all would surely give a thought on it and help me out. I am hereby looking for what are the differences between arraylist and linklist and which one is better suited for what applications ?? I hope i get a good response to this query .This is relatively simply query but stll lots to explore on it. Thanks in adavance, SAurabh
|
Success is not doing extraordinary things but doing ordinary things extraordinarily well.
|
 |
Nathaniel Stoddard
Ranch Hand
Joined: May 29, 2003
Posts: 1258
|
|
Well, first of all, we don't really have a linked list in Java, strictly. ArrayList is an actual class that you can use in your program. Is it implemented using a linked list? Probably. I think so. The point is who knows, and who cares. ArrayList is just a mechanism to treat a container as a resizable array. If you've had a data structures class, you can decide what to use by your performance and complexity needs. It really depends on what what your application needs to do. Depending on that, you can decide which type of container is suitable for your program.
|
Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
|
 |
Saurabh Agrawal
Ranch Hand
Joined: Oct 07, 2003
Posts: 238
|
|
Hello friend , Thanks for your answer but i am looking to a specific answer about the simple differences between a linklist and arraylist and where to use arraylist and where to use link list ?? I hope somebody would revert back to it. Thanks, SAurabh
|
 |
Nigel Browne
Ranch Hand
Joined: May 15, 2001
Posts: 673
|
|
The class java.util.LinkedList implements the List interface and is useful for all stack, queue, deque operations. It has all the usual behaviour expected with a linked list. java.util.ArrayList also implements the List inteface but as resizable Array, because an Array must always have a known capacity there is a time factor involved in the resizing of the Array. ArrayList can be thought of as a non-synchronised Vector Regards Nigel
|
 |
Saurabh Agrawal
Ranch Hand
Joined: Oct 07, 2003
Posts: 238
|
|
Thanks nigel, So what it means is that there is just the difference of dynamisn between linklist and arraylist i.e arraylist size can be dynamically increased.Is it so ? or there are still more major issues worth it.. Looking forward to your response, Saurabh
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
Wow, there's a lot of untruth in this thread.
Well, first of all, we don't really have a linked list in Java, strictly.
Actually, we do: the java.util.LinkedList class.
Is [ ArrayList] implemented using a linked list? Probably. I think so. The point is who knows, and who cares.
Well, actually, it's not, and everyone knows, and everyone should care. ArrayList is well-documented to use an array as a backing store. This has a very specific impact on its performance characteristics. Accessing the 37th element of an ArrayList is very fast. Deleting an element in the middle is slow. This is quite the opposite of how a linked list behaves. That's why there are both: so you can choose which to use based on what's important for your appplication.
So what it means is that there is just the difference of dynamisn between linklist and arraylist i.e arraylist size can be dynamically increased.I
Both an ArrayList and a LinkedList can have elements added to them and/or removed from them, so no, that's not the difference. The difference is the performance, as mentioned above and as described in more detail in the Javadocs. If you need to delete elements in the middle, use a LinkedList. If you need random access, then an ArrayList may be better. Note that LinkedLists have more memory overhead, too.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Wayne L Johnson
Ranch Hand
Joined: Sep 03, 2003
Posts: 399
|
|
Confession ... I chuckled as I read Ernest's reply, to which I say "AMEN!" There are several good tutorials on collections, and a good starting place is SunsSun's collections tutorial. It should answer a lot of your questions. Second, Sun does a great service by providing a (nearly) complete source code with the JDK. So you can look at the source and see exactly what is happening. If you look at the source for java.util.ArrayList, you'll see: whereas for java.util.LinkedList, you'll see: Obviously an ArrayList manages its data using an array, which means as it grows dynamically the array will have to be re-allocated from time to time. The LinkedList is a set of linked "Entry" instances. Choosing the correct collection to use becomes easier as you become more familiar with Java, and seeing the source code can help you make that decision. You can also write a simple application that adds, searches for, iterates through and deletes a bunch of things using different collections, and then seeing how long each takes to run. Good luck with Java, Saurabh ...
|
 |
 |
|
|
subject: Arraylist and linklist !!
|
|
|