Compare the Difference Between Similar Terms

Difference Between Arraylist and Vector

Arraylist vs Vector

An arraylist can be seen as a dynamic array, which can grow in size. Due to this reason, the programmer does not need to know the size of the arraylist when he/she is defining it. Vector can also be seen as an array that can grow in size. Vectors can be easily allocated and can be used to when the required size of the storage is not known until runtime.

What is an Arraylist?

An arraylist can be seen as a dynamic array, which can grow in size. Therefore arraylists are ideal to be used in situation in which you don’t know the size of the elements required at the time of declaration. In Java, arraylists can only hold objects, they cannot hold primitive types directly (you can put the primitive types inside an object or use the wrapper classes of the primitive types). Generally arraylists are provided with methods to perform insertion, deletion and searching. Time complexity of accessing an element is o(1), while insertion and deletion has a time complexity of o(n). In Java, arraylists can be traversed using foreach loops, iterators or simply using the indexes. In Java, arraylists were introduced from version 1.2 and it is part of the Java Collections Framework.

What is a Vector?

Vector is also an array that can grow in size. Vectors can be easily allocated and can be used when the required size of the storage is not known until runtime. Vectors also can only hold objects and cannot hold primitive types. Vectors are synchronized, therefore can be used safely in multithreaded environments. Vectors are provided with methods to add objects, delete objects and search objects. Similar to arraylist in java, vectors can be traversed using foreach loops, iterators or simply using the indexes. When it comes to Java, vectors have been included since the first version of Java.

What is the difference between Arraylist and Vector?

Even though both the arraylists and vectors are very similar to dynamic arrays that can grow in size, they have some important differences. The main difference between arraylists and vectors is that the vectors are synchronized whereas arraylists are unsynchronized. Therefore using arraylists in multithreaded environments will not be suitable, while vectors can be used safely in multithreaded environments (since they are thread safe). But synchronization in vectors would cause a reduction in performance. Therefore it would be not a good idea to use vectors in a single threaded environment. Internally, both arraylists and vectors use arrays to hold objects. When the current space is not enough, vectors will double the size of its internal array, while arraylists increase the size of its internal array by 50%. But when using both the arraylists and vectors, by giving a suitable initial capacity, unnecessary resizing of the internal array can be avoided. In a situation that growth rate of data is known, using vectors would be more suitable since the incremental value of vectors could be defined.