File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Vector vs. Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vector vs. Array" Watch "Vector vs. Array" New topic

Vector vs. Array

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
What is main difference between array and vector?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Well, if you're referring to the classes java.util.Vector and java.util.ArrayList, the difference is pretty simple. A Vector is thread-safe, while an ArrayList is not.
That means that, if multiple threads are sharing data in a Vector, your data will be fine but, if they're sharing data in an ArrayList, you could have data integrity issues.
Of course, this extra safety comes at a price. Vectors are slower than ArrayLists. If you're not sharing data among threads, use an ArrayList, otherwise, use a Vector.
Functionally, they're almost identical.
I hope that helps,

SCJP Tipline, etc.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If you mean arrays and Vectors, the difference is that the size of an array must be allocated prior to use and can not be exapanded. Arrays are much more efficient that Vectors (or any Collection object) and should always be the first choice if possible.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In addition, simple arrays can contain any Java data type, including other arrays. Containers, such as Vectors or ArrayLists can only contain objects (reference variables). You can't have an ArrayList of primitives. Of course, you can have an ArrayList of arrays.
I agree. Here's the link:
subject: Vector vs. Array
jQuery in Action, 3rd edition