• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector vs ArrayList  RSS feed

 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have looked over both classes and haven't really noticed any differences between them. Is there a difference and why would someone use one over the other?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vilanye,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.

From the API Spec:


As of the Java 2 platform v1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized.


From the JavaRanch Journal:


The three implementations of the List interface are Vector, ArrayList and Linked List. Which one you should choose will depend on the requirements of your implementation. An ArrayList is very efficient for getting objects from the list. The LinkedList is more efficient at insertions into and deletions from the list. It also performed very well at iterating through the list. The Vector performed worst of all 3 List objects. The Vector class is less efficient than the ArrayList class mainly because the Vector class is synchronizedI would recommend that that you run your own tests simulating your production environment to determine which List object will perform best for you.

[ January 03, 2006: Message edited by: Corey McGlone ]
 
vivek gaur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 1 more doubt.
what is this synchronized.

y do v say that a vector is synchronized.
 
vivek gaur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 1 more doubt.
what is this synchronized.

y do v say that a vector is synchronized.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Locking an Object for a brief overview on what synchronized does. We say that Vector is synchronized because it's methods are synchronized. It was essentially replaced by ArrayList and I've read somewhere (I think it was something Josh Bloch wrote) the only reason Vector isn't deprecated is compatibility with legacy code. Vector would only be useful if you needed a thread-safe collection and as far as I'm aware there are better and faster solutions in java.util.concurrent if you're 1.5 or by getting a synchronized list from Collections if you're 1.4.
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
the only reason Vector isn't deprecated is compatibility with legacy code.


If by "legacy code" you mean Swing, you'd be correct. Classes like JList and JTable use Vector.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like legacy code to me. Yes, there are various classes written for JDK 1.2 which use Vector, probably because the Swing authors weren't all familiar with the then-new Collections framework. And so we're still stuck with Vector in some cases. Same for Hashtables and Enumerations. Yuck.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They may have said "backwards compatibility" rather than "legacy code" but the semantics seem the same to me. If we really want to know exactly what they meant by it I'd have to find where I read it and ask the author. Perhaps my use of legacy was inappropriate.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vilanye:
I have looked over both classes and haven't really noticed any differences between them. Is there a difference and why would someone use one over the other?


Classes such as Vector, Enumeration and Hashtable are part of the Old Testament, which thou shalt eschew hereinafter. The New Testament introduces ArrayList and a whole happy family of classes, thereby annulling the employ of the previous ones.

So, praise the Lord and use ArrayList!


[ January 05, 2006: Message edited by: Stuart Ash ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!