Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between Vector & ArrayList

 
Verity smith
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All;
Could anybody please explain the difference between a Vector (synchronized) and an ArrayList (not synchronized ).
Thankx in advance!
[ July 12, 2003: Message edited by: Verity smith ]
 
Patrick Cobbett
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've answered your own question. Functionally, the only difference between Vector and ArrayList is that Vecot is synchronized and ArrayList isn't? Vector has a more additional methods which don't really make it any more useful, such as elements() to iterate through the members, which can also be acheived with iterator().
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vectors are an old legacy class which no one really needs any more unless they're writing for JDK 1.1. The world would be a happier place if people just forgot about Vector and Hashtable - use ArrayList and HashMap instead to avoid confusion. If you need synchronization, you should probably emply the synchronized keyword yourself - don't trust Vector or Hashtable as they don't (can't) always put the sync in the right place. (You often need higher-level sync across multiple methods, not just sync within methods.)
In short: Vector - evil. ArrayList - good.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The practical difference between the two is atomic activity. That is, if two threads are sharing the same vector, then any single activity that thread # 1 does with that vector will be visible to Thread #2. Thus, if Thread #1 executes

Then thread #2 will see that "hi" message in the Vector. This is not guaranteed with an ArrayList. So that makes Vectors better, right? Well, not really.
The problem is that most code doesn't need this sort of explicit guarantee, and the overhead of providing such is high. Thus, Jim's statement that Vectors should probably be avoided.
HTH,
M
 
Verity smith
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic