• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Difference between Vector and arrayList  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i just wanted to know difference between arryList and vector.
I want some 4/5 key points for interview or exam point of viwe.

Thanks and regards,
Anil Sonone
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only difference is that the methods in java.util.Vector are synchronized and in java.util.ArrayList not. That's all!
From the performance point of view, this means that java.util.ArrayList should be a little bit faster.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there is another difference, if I remember correctly: they do have different growing algorithms. Take a look at the source code to be sure.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Vector and ArrayList grow at different rates. Also if you use iterator(), an ArrayList's Iterator checks for concurrent modification, and throws ConcurrentModificationException if this is detected. Vector does not. This means that iterating through ArrayList is slightly slower than iterating through a Vector, even without the synchronization. But an ArrayList is better at detecting problems if you're misusing threads on it.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't assume ArrayList is slower when Iterating through it because of the concurrent checks. Syncronization is fairly high on overhead. I would test that, hmmm, maybe I will later. If I do I'll post back the results.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't assume; I have tested it. Though results may vary from one environment to another.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd be iterested in seeing a test. I tried to write up one, but the must be some sort of optimization going on. I create and array of Objects, fill it up with 1000 Objects, then add and iter throught them on both Vector and ArrayList. Recording the time before and after each event using System.currentTimeMillis(). Basically everything took 0 ms. I don't really have the time to go into a more elaberate test so I'll just accept.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Steven Bell:
I'd be iterested in seeing a test.



http://www.coderanch.com/t/202139/Performance/java/Enumeration-vs-Iterator
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!