Vector is type of data structure that implements List Interface. It is very much similar to ArrayList as it also maintains insertion order, that is elements are retrieved in same order as they are added into it.
Like Arrays it has index value that is automatically allocated to it, but main think which distinguishes it is, it can grow or shrink its capacity according to the need after it is created, as it implements growable array.
Important Note: Vector contains many legacy methods that are not part of collection framework which we will discuss below.
Table of Contents
Let us discuss Vector Class with the help of program. In this program we will create an object of Vector Class, some integer values are added and displayed.
Following program has been divided into 3 Steps that we will discuss one by one:
import java.util.*; public class VectorDemo { public static void main(String args[]){ //Step 1: Vector object is created Vector<Integer> vectorObject = new Vector<Integer>(4); //Step 2: Integers are added vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); vectorObject.add(2); vectorObject.add(8); vectorObject.add(8); //Step 3: Values are Displayed System.out.println("Values in Vector Object :" +vectorObject); } }
Output:
Values in Vector Object :[3, 5, 4, 1, 2, 8, 8]
Explanation of Example:
1. boolean add(E e)
This method adds element at the end of the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); vectorObject.add(2); vectorObject.add(8); vectorObject.add(8); System.out.println("Values in Vector Object :" +vectorObject); } }
Output:
Values in Vector Object :[3, 5, 4, 1, 2, 8, 8]
2. void add(int index, E element)
This method adds an element at specified index and moves the whole elements one step in forward direction as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(0,3); vectorObject.add(1,5); vectorObject.add(2,4); vectorObject.add(3,1); for(Integer integer: vectorObject) { System.out.println("Index : " +vectorObject.indexOf(integer)+ " Value: "+integer); } //Adding element at index 2 vectorObject.add(2, 10); System.out.println("\nAfter adding value at index 2:\n"); for(Integer integer: vectorObject) { System.out.println("Index : " +vectorObject.indexOf(integer)+ " Value: " +integer); } } }
Output:
Index : 0 Value: 3 Index : 1 Value: 5 Index : 2 Value: 4 Index : 3 Value: 1 After adding value at index 2: Index : 0 Value: 3 Index : 1 Value: 5 Index : 2 Value: 10 Index : 3 Value: 4 Index : 4 Value: 1
3. void addElement(E obj)
This method adds the specified element at the end of the vector also increasing its size by 1, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Values in Vecor object" +vectorObject); vectorObject.addElement(2); System.out.println("Values in Vecor object" +vectorObject); } }
Output:
Values in Vecor object[3, 5, 4, 1] Values in Vecor object[3, 5, 4, 1, 2]
4. int capacity()
This method gives the capacity of the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("current capacity of Vector object is " +vectorObject.capacity()); } }
Output:
current capacity of Vector object is 4
Read All Vector Methods In JAVA With Example
Premium Project Source Code:
Leave a Reply