Hit enter after type your search item

Vectors in Java – The Dynamic Arrays

The vector class of Java

In the arrays chapter, we learned the standard arrays in Java are of fixed size. Once created, the size of the array cannot be changed or you can say, the elements in the standard array cannot be added or removed after creation.

In Java programming, you will need to work with arrays that are dynamic; that allow adding or removing elements after creation. For that purpose, you may use vectors in Java that implement dynamic arrays.

Before going into further details, have a look at the examples of using the vector class in Java programs.

An example of vector Java class with strings

Before describing how to declare and access vectors, let me show you a few examples of using this in Java programs.

In this example, a vector of string elements is created. Three elements are added by using the add method after vector declaration. This is followed by using the for loop for displaying the elements:

The Java code with vector class:


Java vector

An example of integer type vector class

In the following example, the vector of integer elements is created. Five elements are added in the vector after declaring it by using the add method.

The for-each loop is used for displaying the vector items as shown below:

The code:


vector integer

More about vectors

The vectors are synchronized unlike the ArrayList (that are also dynamic). The vectors use capacity and capacityIncrement for optimizing the storage management. The capacity, which is as large as the vector size is generally larger because the vector size increase in chunks as components are added to the vector.

Also, from Java 2 platform, the vector class implements the List interface. The ArrayList also implements the List interface; however, as mentioned earlier, ArrayList is not synchronized. You should prefer using the vector class if the thread-safe implementation is required. Otherwise, prefer using the ArrayList.

See the following section for demos and code of vector including methods provided by the vector Java class, but first, have a look at how you may declare and access vector elements.

How to declare vector?

Following is the syntax for creating a vector in Java program:

Vector<Integer> vector_name = new Vector<Integer>();

The Vector class is followed by the type and vector name on the left side. On the right side, the new keyword is used which is followed by Vector class. Optionally, you may specify the size of vector, e.g.

Vector<Integer> vector_name = new Vector<Integer>(5);

If you do not specify the data type at the time of declaration, the vector may contain different types of elements like Strings, integers etc.

Creating vector from other collection type

You may also create a vector by using the other collection type like ArrayList. Following is the syntax:

Vector vector_name = new Vector (Collection c) ;

See the following example where I have created an ArrayList with five elements. This is followed by creating a vector where that ArrayList object is specified. Finally, a for loop is used for displaying the elements of that vector:

Code for creating a vector from an ArrayList:


vector from arraylist

A demo of adding elements at specific position by add method

In above examples, the elements in the vector are added by using the add method in sequence. The add method allows adding the elements at the specific position as well. For that, provide the index number and elements to be added in brackets separated by a comma. See the demo and code below:

Java code:


vector add

Getting the length of vector by using size method

The size method returns the total number of elements in the specified vector. See the demo below where I have used the size method twice. One for displaying the total elements in the vector. The other is used in the for loop for specifying the maximum number of times the for loop should run:

The code:


vector size

You saw it returned 4 as the size of the vector.

An example of accessing specific element of vector

By using the get() method, you may access the specific element of the vector. See a demonstration below:

The code:


vector get

The element index is given in the get method and it returned its value which is List in above example.

This div height required for enabling the sticky sidebar