• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector and ArrayList

 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the pros and cons between these two implementation?
they both can be used as dynamic array.

thanks all
tony
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic