Java

What is a Vector in Java?

A vector in Java, is like an array. However, unlike an array, it can shrink or increase in length. Elements can be appended or inserted to the vector, which increases its length. Elements can also be removed, which reduces (shrinks) the vector length.

In Java, there is the class and there are objects from the class. Corresponding objects of a class are instantiated from the class. The vector is a class from which vector objects are instantiated. The vector class is found in the java.util.* package, which has to be imported. This article explains what a vector is and its commonly used methods.

Article Content

Construction of a Vector

A vector can be constructed as an empty vector or with elements. A vector is a collection. This means that a vector can consist of, all integers, or all characters, or all doubles, or all strings, etc. At construction, the type is indicated in angle brackets. The angle brackets take a reference, and not a primitive type. So, it should be <Integer> and not <int>; <Character> and not <char>; <Double> and not <double>; etc. With reference classes, there is no problem. For example, String is just <String> and there is no other alternative. All elements of the vector, are of the same type.

Constructing an Empty Vector

The way to construct an empty vector of characters, is:

Vector<Character> vtr = new Vector<Character>();

where vtr is the name of the vector, given by the programmer. Note the positions of the two angle brackets. Note the presence of the empty parentheses, at the end of the statement. Vector is a class in Java, so it has to begin with an uppercase character. The following program has the creation of a vector:

import java.util.*;

public class TheClass {

public static void main(String[] args) {

Vector<Character> vtr = new Vector<Character>();

}

}

Constructing a Vector with Elements

A non-empty vector can be constructed. The way to do this is similar to the above. It is:

Vector<Character> vtr = new Vector<Character>(al);

were al here, is an ArrayList, another collection. The content of the parentheses has to be the name of a collection. In this case, the ArrayList needs to have been defined as follows:

ArrayList<Character> al = new ArrayList<Character>();

al.add('A'); al.add('B'); al.add('E'); al.add('F');

The following program shows how a non-empty vector can be constructed with this scheme:

import java.util.*;

public class TheClass {

public static void main(String[] args) {

ArrayList<Character> al = new ArrayList<Character>();

al.add('A'); al.add('B'); al.add('E'); al.add('F');

Vector<Character> vtr = new Vector<Character>(al);

}

}

Note: ArrayList is also in the java.util.* package.

Adding Elements to a Vector

After a vector has been created, whether empty or not, elements can be added.

Adding Elements One-by-One

The syntax for adding elements one-by-one is:

public boolean add(E e)

It returns true, if a change took place, and false otherwise. The following code in the main() method, shows how elements are added to a vector.

Vector<Character> vtr = new Vector<Character>();

vtr.add('A');

vtr.add('B');

vtr.add('E');

vtr.add('F');

The vector now consists of the characters, ‘A’, ‘B’, ‘E’, and ‘F’.

Adding a List of Elements

More than one element can be added at the same time, from another vector. The syntax for the vector method for this is:

public boolean addAll(Collection<? extends E> c)

It returns true, if a change took place.

The following code in the main() method, illustrates this:

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('E'); vtr.add('F');

Vector<Character> c = new Vector<Character>();

c.add('G'); c.add('H');

vtr.addAll(c);

The other vector here is c.

Appending to a Vector

The above two ways of adding to a vector is appending: adding elements at the end.

The Length of a Vector

The length of a vector is the size of the vector: the number of elements the vector has. The vector class has a method to obtain the length of a vector. The full syntax is:

public int size()

The size returned is an integer. Consider the following code in the main() method:

Vector<Character> vtr1 = new Vector<Character>();

Vector<Character> vtr2 = new Vector<Character>();

vtr2.add('A'); vtr2.add('B'); vtr2.add('E'); vtr2.add('F');

 

int len1 = vtr1.size();

int len2 = vtr2.size();

System.out.println("len1 is: " + len1);

System.out.println("len2 is: " + len2);

The output is:

len1 is: 0

len2 is: 4

Accessing an Element

Accessing an element means getting (reading) or setting (changing) the value of an element in the vector.

Getting Element

The full syntax to get an element is:

public E get(int index)

A copy of the element is returned. Index counting begins from 0. The following code gets all the elements of the vector using a for-loop:

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('E'); vtr.add('F');

 

for (int i=0; i<vtr.size(); i++) {

char ch = vtr.get(i);

System.out.print(ch); System.out.print(", ");

}

System.out.println();

The output is:

A, B, E, F,

Setting Element

The full syntax to set (change) an element is:

public E set(int index, E element)

It returns the old element at the index position. The following code sets (changes) all the elements of the vector:

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('E'); vtr.add('F');

vtr.set(0, 'W'); vtr.set(1, 'X'); vtr.set(2, 'Y'); vtr.set(3, 'Z');

 

for (int i=0; i<vtr.size(); i++) {

System.out.print(vtr.get(i)); System.out.print(", ");

}

System.out.println();

The output is now:

W, X, Y, Z,

with all elements changed.

Inserting Elements

Elements can be inserted into a vector. This needs two other kinds of add() method.

The add(int index, E element) Method

This method inserts one element at an index position. The element that was at that position is shifted one place to the right. The full syntax for this method is:

public void add(int index, E element)

In the following code, a character is inserted at index 2 position:

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('E'); vtr.add('F');

vtr.add(2, 'D');

 

for (int i=0; i<vtr.size(); i++) {

System.out.print(vtr.get(i)); System.out.print(", ");

}

System.out.println();

The original array order is:

A, B, E, F

The output is:

A, B, D, E, F,

‘D’ has been inserted at index 2.

addAll(int index, Collection<? extends E> c)

This can insert a list at the index position, pushing the rest of the elements, that were on the right of that position (including the element at the position), further to the right, by the number of characters in the inserted list. The list to be inserted can be another vector (collection). The full syntax of this method is:

public boolean addAll(int index, Collection<? extends E> c)

It returns true, if a change took place; false otherwise. The following code illustrates its use:

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('E'); vtr.add('F');

Vector<Character> c = new Vector<Character>();

c.add('C'); c.add('D');

vtr.addAll(2, c);

 

for (int i=0; i<vtr.size(); i++) {

System.out.print(vtr.get(i)); System.out.print(", ");

}

System.out.println();

The original array order is:

A, B, E, F

The output is:

A, B, C, D, E, F,

The list [‘C’, ‘D’] has been inserted at index 2.

Removing Elements

There are many ways to remove elements. Only two approaches are addressed here.

remove(int index)

This method removes the element at the particular index. The rest of the elements on the right are shifted to the left by one place. The full syntax for this method is:

public E remove(int index)

It returns the element that was removed. The following code illustrates its use:

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('C'); vtr.add('D'); vtr.add('E'); vtr.add('F');

vtr.remove(3);

 

for (int i=0; i<vtr.size(); i++) {

System.out.print(vtr.get(i)); System.out.print(", ");

}

System.out.println();

The original array order is:

A, B, C, D, E, F

The output is:

A, B, C, E, F,

‘D’ at index 3, has been removed.

vtr.subList(int fromIndex, int toIndex).clear()

This approach removes a range of elements from fromIndex inclusive, to toIndex exclusive. In the following code, fromIndex is 2 and toIndex is 4. So, only elements at index 2 and 3 are removed. The code is:

import java.util.*;

public class TheClass {

public static void main(String[] args) {

Vector<Character> vtr = new Vector<Character>();

vtr.add('A'); vtr.add('B'); vtr.add('C'); vtr.add('D'); vtr.add('E'); vtr.add('F');

vtr.subList(2, 4).clear();

 

for (int i=0; i<vtr.size(); i++) {

System.out.print(vtr.get(i)); System.out.print(", ");

}

System.out.println();

}

}

The original array order is:

A, B, C, D, E, F

The output is:

A, B, E, F,

The list of consecutive characters [‘C’, ‘D’], has been removed.

Conclusion

A vector in Java, is like an array. However, unlike an array, it can shrink or increase in length. Elements can be appended or inserted to the vector, which increases its length. Elements can also be removed, which reduces the length. This article has explained what a vector is, and its commonly used methods.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.