Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array lists vs Vectors

 
sujay kulkarni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when to use arraylist and when to use Vectors ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moving to beginning java forum.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only use Vector when you have no choice, when interacting with code that requires it.

Vector was created before the Collections Framework was added in 1.2, and was then retrofitted into it. ArrayList was created as part of the Collections framework from its beginning. Though both classes implement List, Vector also has a bunch of methods that are not part of the standard List interface, going back to its days before the Framework was defined. Vector will function just as well as ArrayList in most or all situations, but the general consensus now is to avoid it. If somebody sees it in your code, they're going to wonder why you're using it--whether you had a good reason to use it, or if you just didn't know that ArrayList is now preferred.
 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use ArrayLists when you need any old ordered collection, or when you need good random-access times.

Use Vectors when the API you're working with specifically needs them. I believe this is still the case for some classes in the Swing API, for example. *Never* use them when you're working with new code.

Other alternatives are always better than Vector. For regular use, ArrayList is better. For synchronized use with many edits, use Collections.synchronizedList(arrayList). For synchronized use with many traversals, use CopyOnWriteArrayList.
 
sujay kulkarni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton !! everyone for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic