File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anybody tell me the difference ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anybody tell me the difference ?" Watch "Anybody tell me the difference ?" New topic
Author

Anybody tell me the difference ?

Preszio let
Greenhorn

Joined: May 06, 2003
Posts: 4
Hi
can anybody tell me the difference between
LinkedList , ArrayList and Vector
Thanks !!
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
Well in simple terms the main difference between
ArrayList,Vector with LinkedList is linkedlist is sequetinal access mechanisam. Hence linkedlist is preferred where you wish to get the list of values in a sequential manner and Arraylist/vector is perferred where you need random lookup of values.
now the difference between vector and arraylist is vector is syncronized where as arraylist is not.
Hope this helps


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Preszio let
Greenhorn

Joined: May 06, 2003
Posts: 4
thanks Lawrence.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
You can also look at some Newsletter articles:
Collections 1
Collections 2
The first artciles covers lists while the second one covers sets.
I hope that helps,
Corey


SCJP Tipline, etc.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3295
    
    7
Originally posted by Lawrence Chettiar:
Hence linkedlist is preferred where you wish to get the list of values in a sequential manner and Arraylist/vector is perferred where you need random lookup of values.

That is not really correct. Both, ArrayList and LinkedList are Lists and thus store the values in an ordered fashion. Search for a random element will require a serial lookup through the whole list.
Advantages and disadvantage of ArrayList and LinkedList arise from the fact that ArrayList is based on an array while LinkedList is beased on a linked list. Therefore, adding and removing elements from the middle of the list will be faster in a LinkedList than in an ArrayList (because of the shift of element positions). For this reason, stack style operation are faster in a LinkedList.
Memorywise, ArrayList should be more efficient than a LinkedList because LinkedList stores two references (front and back) per element. ArrayList will also be more efficient if you want to add tons on elements in one shot and then just reading the List (not adding/removing values).
HTH,
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
LinkedList is much poorer than ArrayList for access by means of an index as it requires a sequential scan from the front or back of the list, ie not random access at all. ArrayList has fast random access as it is backed by an array, whereas LinkedList is doubly-linked list.
Both are pretty good at iterating through a list, both retrieve in insertion order.
What hasn't been mentioned yet is that a LinkedList can simulate a stack, queue or double-ended queue (deque). So, if you want to implement something like a FIFO queue, then linkedLIst has the methods to add and remove the first elements in the list.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you look into the two articles I indicated earlier, you'll find actual benchmarks for performance of the various collections. Perhaps perusing those articles would be worth-while.
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Hi

Do you think Performance is included in SCJP ?


SCJP 2 1.4
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Let me summarize all your tots
LinkedList
-Sequential Access
-Non-synchornized
-Use index
-FIFO, stack, dequeu
Vector
-Random Access
-Synchronized
ArrayList
-Random Access
-Non-synchronized
-resize itself
Anymore things to add ?
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Just read the two articles, do you have the Map interface version.
Both articles are great !!
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97
The Map article is in the August 2002 newsletter at : Collections3
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Robbie,
ArrayList is the modern version of Vector. It is faster, largely because it is not synchronized.
Use ArrayList for new code. If you have to use Vector, normally because you are modifying existing code or dealing with some method which requires a vector, try and get accustomed to the new way of working. You can do this because Vector has been re-engineered to bring it into line with the Collections Framework way of working. For instance, use List's add method rather than addElement to add an element to the end of the vector. Or use iterator to return an Iterator rather than elements to return an Enumeration.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You should never use Vector. If you need a synchronized List then use the synchronization method of the Collections class:
The fourth and final article


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anybody tell me the difference ?