| Author |
Vector and ArrayList
|
Anthony Karta
Ranch Hand
Joined: Aug 09, 2004
Posts: 342
|
|
what are the pros and cons between these two implementation? they both can be used as dynamic array. thanks all tony
|
SCJP 5
|
 |
Petr Blahos
Ranch Hand
Joined: Apr 28, 2004
Posts: 131
|
|
Vector and ArrayList are essentially the same, but Vector is synchronized. As it is usually more convenient to do synchronization on a higher level, you will probably prefer ArrayList. Even though Vector implements List, Vector is older than Collections framework and the List support was added in it later. What you should do (and what you probably already do) is to declare a variable as List (instead of Vector or ArrayList). There is a nice tutorial on Collections on java.sun.com. P.
|
Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
|
 |
Anthony Karta
Ranch Hand
Joined: Aug 09, 2004
Posts: 342
|
|
Hi, thanks for your reply. But I'm not really get it. what do you mean by "synchronization"? when/in what condition do we need it or it will happen? hmm what benefits of List / ArrayList compare to Vector other than Vector is an old implementatin? I found it's a bit confusing study Java because there could be more than one implementation but they do the same job - make learning curve more harder! thanks again tony
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
synchronisation has to do with multithreaded code. If you don't need it, use ArrayList for the increased performance. If you DO need it, still use ArrayList but through the synchronisation wrapper provided (check the API docs). Vector is a dinosaur from before the Collections framework existed and kept on mainly for backwards compatibility.
|
42
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
... and remember: if you are writing multi-threaded code and do need synchronization, it is very unlikely that synchronizing your List will make your code threadsafe. Most of the time it's coarser-grained business operations that need to be atomic, not the little List methods. - Peter
|
 |
 |
|
|
subject: Vector and ArrayList
|
|
|