Vector Class in Java

In this article, we will learn about Vector Class in Java.

The Vector class implements an upgradeable array of objects. Vectors mostly belong to inherited classes but are now fully compatible with collections. It’s in the java.util.package and implements the List interface, so we can use all the methods of the List interface here.

  • Vector implements a dynamic array, which means it can be expand or shrunk as need. Like an array, it contains components that can be accessed via an integer index.
  • They are very similar to ArrayList, but Vector is synced and has an inherited method that the collection framework does not include.
  • It also maintains an insertion order like an ArrayList, but it is rarely used in an unthreaded environment because it is synchronized and because of this, it has poor performance in adding, finding, deleting, and updating its items.
  • The iterators returned by the Vector class are infallible. If changed at the same time, it fails and throws the ConcurrentModificationException exception.

Declaration:

public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Here E is the type of the element

  • It extends AbstractList and implements list interfaces.
  • It implements the interfaces Serializable, Cloneable, Iterable, Collection, List, RandomAccess.
  • The directly known subclass is Stack.

Constructors

1. Vector (): Creates a standard vector, the initial capacitance is 10.

Vector<E> v = new Vector<E>(); 

2. Vector (int size): Creates a vector, the initial capacity of which is given by the size.

Vector<E> v = new Vector<E>(int size); 

3. Vector (int size, int incr): Creates a vector whose initial capacity is given by size and increment by incr. It indicates the number of elements to be assign each time a vector is scal up.

Vector<E> v = new Vector<E>(int size, int incr);  

4. Vector (Collection c): The elements of collection c that contain create a vector.

Vector<E> v = new Vector<E>(Collection c); 

Example

 import java.io.*;
import java.util.*;

class Vector.Example {
	
	public static void main(String[] args)
	{
		// Size of the  Vector
		int n = 5;

		// Declaring the Vector with  initial size n
		Vector<Integer> v = new Vector<Integer>(n);

		// Appending new elements at  the end of the vector
		for (int i = 1; i <= n; i++)
			v.add(i);

		System.out.println(v);
 
		v.remove(3);

		// Displaying the vector  after deletion
		System.out.println(v);  
		for (int i = 0; i < v.size(); i++)
			System.out.print(v.get(i) + " ");
	}
}  

Output

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5

That’s all we will learn about Vector Class in Java.

Next Article

1. PriorityQueue in Java
2. Stack Class in Java
3. Queue Interface In Java
4. List Interface in Java with Examples

Please share this post:
Posts created 72

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: