Vectors can be used to create a dynamic array that can hold objects of any type and any number. The objects do not have to be homogenous (same). Vector class contained in the java.util package.
Vector can be declared as follows:
Vector list = new vector(); //Declaring vector without size is known as explicit declaration. Vector list = new vector(5); //Declaring vector with size is known as implicit declaration.
Advantages of Vector over Array:
Example 1: WAP to convert a string vector into an array of strings and displays the strings.
import java.util.*; class languagevector { public static void main(String args[]) { Vector list = new Vector(); int length = args.length; for(int i=0; i<length; i++) { list.addElement(args[i]); } list.insertElementAt("COBOL",2); int size = list.size(); String listArray[] = new String[size]; list.copyInto(listArray); System.out.println(“List of Language”); for(int i=0; i<size; i++) { System.out.println(listArray[i]); } } }