• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList / Vetor ?

 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wat are the differences between ArrayList and Vector ? and which is better for what purpose ?
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides having more methods to play with, Vectors are synchronized, where as ArrayLists are not. I am more familiar with ArrayList than Vector, so I can't really give any good examples of why you would use one over the other, but I would venture a guess that if you want a synchronized List that multiple threads could modify, you would use a Vector. See the API at http://java.sun.com/products/jdk/1.2/docs/api/index.html .
Someone else could probably give concrete examples,
Jason
[This message has been edited by jason adam (edited September 04, 2001).]
 
Amit Agrawal
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jason. but would be glad to know if there are some more differnces which may help in selecting between Vector and ArrayList.
Regards,
Amit, New Delhi.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayLists are favoured more now because they are part of the Collections classes and fit in more with the new Java 1.2+ API... Vectors are left over from earlier versions, and should only be used in two cases I can think of:

  • Applets - since browser JVMs use pre-1.2 APIs...
  • Threads - if you need a synchronized ArrayList and are too lazy to make your own...


  • -Nate
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to the above, it is worth mentioning that in cases where a synchronized collection is NOT required, you should ALWAYS prefer an ArrayList to a Vector, since ArrayList provides quicker access due to the lack of overhead w/regard to obtaining locks before executing each method.
In a simple example in Peter Haggar's "Practical Java" book, the ArrayList code is 4 times faster than the equivalent code using Vector.

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic