File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector vs. Array

 
Mike Shn
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is main difference between array and vector?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Corey
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Corey
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic