This post will discuss the difference between an array and a Vector in Java.

⮚ Resize

The length of an array is fixed once it is created, and elements cannot be added or removed before its creation.

A Vector is a resizable-array that works by reallocating storage and copying the old array elements to a new array.

⮚ Synchronization

A Vector is synchronized, whereas an array is not synchronized.

⮚ Performance

Both arrays and Vector take constant time for retrieval and assignment operations, but Vector is relatively slow as it is synchronized.

Also, insertions at the end of the Vector require only amortized constant time, i.e., adding n elements requires Θ(n) time.

⮚ Storage

An array does not reserve any additional storage while a Vector reserve O(n) additional storage. We can use trimToSize() method of the Vector class to remove any additional storage.

⮚ Primitives

Java arrays can hold both primitive data types (int, char, long, etc.) and Java objects (Integer, Character, Long, etc.), whereas a Vector can hold only Java objects.

⮚ Size

To find the size of the Vector, we can call its size() method, whereas an array has a length property that stores its length.

⮚ Dimensions

The dimension of an array is the total number of indices needed to select an element. Arrays in Java supports single-dimensional array as well as multidimensional arrays. A Vector has no concept of dimensions, but we can easily construct a Vector of Vectors.

⮚ Support for Generics

Vector supports generics to ensure type-safety while an array does not support generics.

That’s all about the differences between an array and Vector in Java.

 
Reference: Arrays Wiki