What is vector? How to use vector that may be vary in size? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is vector? How to use vector that may be vary in size?

Java

29th Aug 2017, 2:31 AM
Sreejith P
Sreejith P - avatar
3 Answers
+ 3
Vector is just an Object that deals with the array for you. Same with array list. So, you can do things like: myVector.add(ObjectName); And it will append it to the array. The Vectors/Arraylists use a partially filled array, when full, they create a new array of larger size and deep copy all elements over to the larger array.
29th Aug 2017, 4:51 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
The arrayList that is resizable. I leave you official documentation https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
29th Aug 2017, 3:21 AM
Sebastian Barreiro
Sebastian Barreiro - avatar
0
Vector and ArrayList are almost equivalent. The difference is that access to a Vector is synchronized, whereas access to an ArrayList is not. What this means is that only one thread can call methods on a Vector at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList, this isn't the case. Generally, you'll want to use an ArrayList; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector. http://net-informations.com/java/col/arraylist.htm
19th Jul 2018, 5:28 AM
rahul kumar
rahul kumar - avatar