×

Vector in Java

Java Vector Class

The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of a Vector can grow or shrink as required to add and remove items after the Vector created.

Vectors are similar to ArrayList, but Vector is synchronized. It implements the List interface and extends AbstractList.

Vector tries to optimize storage by maintaining a capacity and capacityIncrement. Always, the capacity at least as large as the size of the vector. To reduce the amount of incremental reallocation, the methods usually increases the capacity of a vector before inserting a large number of elements.

Java Vector Fields:

capacityIncrement It increases the capacity of the vector automatically when its size becomes greater than its capacity.
elementCount It returns the valid components in this vector object.
elementData The components of the vector are stored in this array buffer.

Constructors of Vector and its usages:

Vector() It constructs an empty vector with size 10 and with zero standard capacity increment.
Vector(Collection c) It creates a vector with elements of the specified collection in the arguments; in the order, they are returned by the collection’s iterator.
Vector(int initialCapacity) It constructs an empty vector with specified initial capacity in the argument, with capacity increment equals to zero.
Vector(int initialCapacity, int capacityIncrement) It constructs an empty vector with specified initial capacity and capacity increment.

Methods of Java Vector and their usages:

Modifier and Data type Methods Description
Boolean add(E e) It adds the specific element to the end of the Vector
addAll(Collection c) It will add all the elements in the specified collection to the end of the Vector, in the same order as it is in the specified collection.
addAll(int index, Collection c) It adds all the elements in the vector from the specified collection at the specified index position in the arguments.
contains (Object o) It will return true if the specified element present in the vector
containsAll(Collection c) Will return true if all the elements in the specified collection are present in the vector.
equals(Object o) It will compare the specified object with the vector for equality.
isEmpty() Used to test if the vector has no components.
remove(Object o) It will delete the first occurrence of the specified element in the vector. In case the element is not present, then status will be unchanged.
removeAll(Collection c) Removes all the Vector elements that are present in the specified collection.
removeElement(Object obj) It will remove the first occurrence (from lowest index) of the argument from the vector
retainAll(Collection c) It will keep only the elements in the vector that are contained in the specified Collection.
int capacity() It will return the current capacity of the vector.
hashCode() It returns the hash value of the vector.
indexOf(Object o) It will return the index of the first occurrence of the element specified in the argument or returns –ve one if the element is not present in the vector.
indexOf(Object o, int index) It will return the index of the first occurrence of the element specified in the argument, searching forward from index or return -1 if the element is not present in the vector.
lastIndexOf(Object o) It will return the index of the last occurrence of the element specified in the argument or -1 if the element is not in the vector.
lastIndexOf(Object o, int index) It also returns the last occurrence of the element specified in the argument but searching backward from the index or it returns -1 if the vector does not contain the specified element.
Size( ) Used to get the number of elements present in the vector.
void add(int index, E element) It will insert the specified element at the specified position in the vector.
addElement(E obj) It appends the specified component at the end of the vector, increasing its size by 1.
clear() Used to remove the elements from the vector.
copyInto(object[] anArray) It will copy the elements of the vector to the specified array.
ensureCapacity(int minCapacity) Used to increase the capacity of the vector if necessary, to ensure that it holds the number of elements specified by the minCapacity argument.
insertElementAt(E obj, int index) Used to insert the specified object as an element in the vector at the specified index position.
removeAllElements() Removes all elements from the vector and set the size to zero.
removeElementAt(int index) It deletes the element at the specified index location.
removeRange(int fromIndex, int toIndex) Used to remove all the element from the list whose index is between fromIndex(inclusive) and toIndex(exclusive).
setElementAt(E obj, int index) Used to set the element at the specified index of this vector to be the specified object.
setSize(int newSize) It sets the size of the vector to the specified argument.
trimToSize() It will trim the capacity of the vector to be the vectors current size.
Object clone() Used to clone the vector/exact copy of the vector
toArray() It always returns an array containing all the values in the vector in the sequence.
String toString() It returns an array that contains all the elements in the vector in the correct order.
E elementAt(int index) It returns the elements at the specified index.
firstElement() It returns the first element of the vector (item at index 0).
get(int index) It will return the values at the specified position in the vector.
lastElement() It will return the last element of the vector.
remove(int index) It is used to remove the element from the specified position in the vector.
set(int index, E element) It will replace the element at the specified position in the vector with specified elements in the argument.
Enumeration elements() It will return an enumeration of the elements of this vector.
Iterator iterator() Always returns an iterator over the elements in the list in proper sequence.
ListIterator listIterator() It always returns a ListIterator over the values in the list (in sequence).
ListIterator listIterator(int index) It always returns a list iterator over the elements in the list, starting at the specified position
List sublist(int fromIndex, int toIndex) It will return a view of the area of the list between the starting and ending specified index position.
T[] toArray(T[] a) It returns an array that contains all the elements of the vector in the sequential order, and the runtime type of the returned array is that of the specified array.

Example to illustrate add() method.

import java.util.*;
class Add_demo {
public static void main(String[] arg)
{
// creating default vector
Vector a = new Vector();
//adding element to the vector using add()
a.add("Learn");
a.add("Tutorials");
a.add("And");
a.add("Examples");
System.out.println("Vector is: " + a);
}
} 

Output:

Vector is:  [Learn, Tutorials, And, Examples]

Example to illustrate addAll(Collection c) method.

 import java.util.*;
class add_demo {
public static void main(String[] arg)
{
ArrayList ar = new ArrayList();
ar.add("Tutorials");
ar.add("And");
ar.add("Examples");
// creating default vector
Vector v = new Vector();
// copying all elements of array list into vector
v.addAll(ar);
// checking vector v
System.out.println("vector v:" + v);
}
} 

Output:

vector v:[Tutorials, And, Examples]

Example to Demonstrate addAll(int index, Collection c) method.

 import java.util.*;
class add_demo2 {
public static void main(String[] arg)
{
ArrayList ar = new ArrayList();
ar.add("Learn");
ar.add("Tutorials");
ar.add("And");
ar.add("Examples");
// creating default vector
Vector v = new Vector();
v.add("Java Vector");
// copying all element of arraylist into vector
v.addAll(1, ar);
// printing vector
System.out.println("vector v:" + v);
}
} 

Output:

vector v:[Java Vector, Learn, Tutorials, And, Examples]

Example to illustrate contains() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// creating default vector
Vector v = new Vector();
v.add("Java");
v.add("OOPS");
v.add("Hibernate");
v.add("Spring");
v.add("Servlet");
// checking whether vector contains "HIbernate" or not
if (v.contains("Hibernate"))
System.out.println("Hibernate exists in the List");
}
} 

Output:

Hibernate exists in the List

Example to illustrate containsAll() method.

import java.util.*; 
public class Demo {
public static void main(String arg[]) {
//Creating an empty Vector
Vector vc = new Vector();
//Adding elements in the vector
vc.add("Java");
vc.add("Spring");
vc.add("boot");
vc.add("hibernate"); 
//Creating an empty list
List list = new ArrayList();
//Adding elements in the ArrayList
list.add("Java");
list.add("hibernate");
//checking if the vector contain the item present in the arraylist
System.out.println("Vector contains all the list item.- "+vc.containsAll(list));
//adding one more unique element to the list
list.add("Ajax");
//checking again for the presence of element
System.out.println("Vector contains all the list item.- "+vc.containsAll(list));
}
} 

Output:

Vector contains all the list item.- true
Vector contains all the list item.- false 

Example to illustrate equals() method

import java.util.*; 
class Demo {
public static void main(String[] arg)
{
// creating default vector
Vector v = new Vector();
v.add("Java");
v.add("Spring");
v.add("Hibernate");
v.add("Jquery");
v.add("Servlet");
// creating second vector
Vector v_second = new Vector();
v_second.add("Java");
v_second.add("Spring");
v_second.add("Hibernate");
v_second.add("Servlet");
v_second.add("Jquery");
//checking if both the vectors have same elements
if (v.equals(v_second))
System.out.println("both vectors are equal");
else
System.out.println("Vectors are not equal");
}
} 

Output:

Vectors are not equal

Example to illustrate isEmpty() method.

import java.util.*; 
class IsEmptyDemo {
public static void main(String[] arg)
{
// creating default vector
Vector v = new Vector();
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
v.clear();
// checking whether vector is empty or not
if (v.isEmpty())
System.out.println("Vector is empty");
}
} 

Output:

Vector is empty

Example to illustrate remove() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// create default vector of capacity 10
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("C");
v.add("D");
v.add("E");
// removing element from 1st index position
v.remove(1);
// checking vector
System.out.println("after removal: " + v);
}
} 

Output:

after removal: [A, C, D, E]

Example to illustrate removeElement() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
Vector v = new Vector(5);
//using add() method to add elements
v.add("Java");
v.add("Android");
v.add("Spring");
v.add("Pyhton");
v.add("MongoDB");
// removing an element from vector
v.removeElement("Java");
// checking vector
System.out.println("Vector after removal: " + v);
}
} 

Output:

Vector after removal: [Android, Spring, Pyhton, MongoDB]

Example to illustrate retainAll() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
Vector v = new Vector(5);
Vector vretain = new Vector();
// using add() method to add elements
v.add("Java");
v.add("Spring");
v.add("AI");
v.add("MongoDB");
v.add("Oracle");
// these elements will be retained
vretain.add("Java");
vretain.add("Oracle");
vretain.add("Spring");
System.out.println("Calling retainAll()");
v.retainAll(vretain);
//printing all the elements available in vector
System.out.println("Elements after removal :- ");
//using iterator to return the elements in the sequence they are present int the vector v
Iterator itr = v.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
} 

Output:

Calling retainAll()
Elements after removal :-
Java
Spring
Oracle 

Example to illustrate capacity() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
Vector v = new Vector();
// using add() to add elements in the vector
v.add("A");
v.add("B");
v.add("C");
v.add("D");
v.add("E");
// printing the capacity of vector v
System.out.println("Capacity of vector is: " + v.capacity());
}
} 

Output:

Capacity of vector is: 10
//The output is 10 because the default capacity of a vector is 10. 

Example to illustrate hashCode() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
Vector v = new Vector(5);
// using add() to add elements in the vector
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
// checking hash code
System.out.println("Hash code: " + v.hashCode());
}
} 

Output:

Hash code: 29615266

Example to illustrate indexOf() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// create default vector of capacity 10
Vector v = new Vector();
v.add("PL/Sql");
v.add("Sql");
v.add("MongoDB");
v.add("Oracle");
v.add("MySql");
// get the element at index of Geeks
System.out.println("Index of Oracle is: " + v.indexOf("Oracle"));
}
} 

Output:

Index of Oracle is: 3

Example to illustrate lastIndexof() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// create default vector of capacity 10
Vector v = new Vector();
v.add(1);
v.add(2);
v.add(5);
v.add(5);
v.add(2);
// checking last occurance of 5
System.out.println("Last occurrence of 5 is: " + v.lastIndexOf(5));
}
} 

Output:

Last occurrence of 5 is: 3

Example to illustrate size() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// creating default vector
Vector v = new Vector();
v.add("Java");
v.add("Python");
v.add("MongoDB");
v.add("Oracle");
v.add("Ruby");
// Printing size of vector
System.out.println(" Size of vector: " + v.size());
}
} 

Output:

Size of vector: 5

Example to illustrate add(int index, Object obj) method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// create default vector
Vector v = new Vector();
v.add(0, "India");
v.add(1, "Australia");
v.add(2, "England");
v.add(3, "South Africa");
v.add(4, "Pakistan");
System.out.println("Vector is " + v);
}
} 

Output:

Vector is [India, Australia, England, South Africa, Pakistan]

Example to illustrate addElement(E obj)

import java.util.*;
class Demo {
public static void main(String args[])
{
// Creating an empty Vector
Vector v = new Vector();
// Using add() to add elements in the vector
v.add("Tutorials");
v.add("And");
v.add("Example");
v.add("Java");
v.add("Python");
// Printing the present vector
System.out.println("The vector is: " + v);
// Adding more elements to the end of the vector
v.addElement("Machine Learning");
v.addElement("AI");
// Printing the modified vector
System.out.println("The new Vector is: " + v);
}
} 

Output:

The vector is: [Tutorials, And, Example, Java, Python]
The new Vector is: [Tutorials, And, Example, Java, Python, Machine Learning, AI] 

Example to Illustrate void clear() method.

import java.util.*;
class Demo {
public static void main(String[] arg)
{
// creating default vector
  Vector v = new Vector();
  v.add(0, "Java");
  v.add(1, "Spring");
  v.add(2, "Hibernate");
  v.add(3, "MongoDB");
  v.add(4, "Oracle");
  System.out.println("Vector is: " + v);
  // clearing the vector using clear() method
  v.clear();
  // checking vector
  System.out.println("After clearing: " + v);
   }
  } 

Output:

Vector is: [Java, Spring, Hibernate, MongoDB, Oracle]
After clearing: [] 

Example to illustrate copyInto() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        Vector v = new Vector(5);
        // using add() to add elements in the vector
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        Integer[] ar = new Integer[5];
        // copying componnents of vector in array
        v.copyInto(ar);
        System.out.println("elements in array ar: ");
        for (Integer num : ar) {
            System.out.println(num);
        }
    }
} 

Output:

1
2
3
4
5 

Example to illustrate ensureCapacity() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // creating default vector
        Vector v = new Vector();
        // ensuring the capacity
        v.ensureCapacity(20);
        // cheking capacity
        System.out.println("Minimum capacity: " + v.capacity());
    }
} 

Output:

Minimum capacity: 20

Example to illustrate insertElementAt() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        Vector v = new Vector(5);
 //using add() to add elements in the vector
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        // inserting 20 at the index 5
        v.insertElementAt(10, 5);
        // printing vector
        System.out.println(" Vector: " + v);
    }
} 

Output:

Vector: [1, 2, 3, 4, 5, 10]

Example to illustrate removeAllElements() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        Vector v = new Vector(5);
        // using add() method to add elements
        v.add("A");
        v.add("B");
        v.add("C");
        v.add("D");
        v.add("E");
        // removing all elements
        v.removeAllElements();
        // checking size of vector
        System.out.println("Size: " + v.size());
        // checking components of vector
        System.out.println("Elements in Vector " + v);
    }
} 

Output:

Size: 0
Elements in vector: [] 

Example to illustrate setElementAt() method

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // creating default vector
        Vector v = new Vector();
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Ruby");
        v.add("Django");
        // setting MongoDB at the place of Ruby
        v.setElementAt("MongoDB", 3);
        System.out.println("vector: " + v);
    }
} 

Output:

vector: [Java, Python, Android, MongoDB, Django]

Example to illustrate setSize() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // creating default vector
        Vector v = new Vector();
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Ruby");
        v.add("Django");
        // setting the new size of vector
        v.setSize(13);
        // printing the size of vector
        System.out.println("size of vector: " + v.size());
    }
} 

Output:

size of vector: 13

Example to illustrate trimToSize() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // creating default vector of capacity 10
        Vector v = new Vector();
        v.add("Ankit");
        v.add("Aman");
        v.add("Deepanshu");
        v.add("Priyanshu");
        v.add("Himanshu");
        //initial capacity
        System.out.println("Initial capacity of Vector: " + v.capacity());
        // trimming the capacity to size
        v.trimToSize();
        // Printing capacity after trimming
        System.out.println("Capacity of vector after trimming: " + v.capacity());
    }
}

Output:

Initial capacity of Vector: 10
Capacity of vector after trimming: 5 

Example to illustrate clone() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // create default vector
        Vector vector = new Vector();
        Vector vector_clone = new Vector();
        vector.add("Java");
        vector.add("Python");
        vector.add("Android");
        vector.add("Ruby");
        vector.add("Django");
        vector_clone = (Vector)vector.clone();
        // printing cloned vector
        System.out.println("Clone of vector: " + vector_clone);
    }
} 

Output:

Clone of vector: [Java, Python, Android, Ruby, Django]

Example to illustrate toArray() method.

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      // creating an empty Vector with initial capacity 5   
      Vector v = new Vector(5);   
      // creating an array with capacity 5
      String[] vArray = new String[5];
      // using add() to add elements in the vector
      v.add("A");
      v.add("B");
      v.add("C");
      v.add("D");
      v.add("E");
      // filling array from the vector
      v.toArray(vArray);
      // checking the content of the array
      System.out.println("Elements are: "); 
      for (int i = 0; i < vArray.length; i++) {
         System.out.println(vArray[i]);
      }
   }   
} 

Output:

Elements are:
A
B
C
D
E 

Example to illustrate toString() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // creating default vecto
        Vector v = new Vector();
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        // priting equivalent string value for the vector
        System.out.println(" String equivalent of vector are: " + v.toString());
    }
} 

Output:

String equivalent of vector are: [1, 2, 3, 4, 5]

Example to illustrate elementAt(int Index) method.

import java.util.Vector;
 class Demo {
   public static void main(String[] args) {
      // creating an empty Vector v with an initial capacity of 4     
      Vector<Integer> v = new Vector<Integer>(4);
      //using add() to add elements in the vector
      v.add(20);
      v.add(100);
      v.add(120);
      v.add(50);
      //printing the element at 3rd index position in the vector
      System.out.println("Element at 3rd index position :- "+v.elementAt(3));
   }   
} 

Output:

Element at 3rd index position :- 50

Example to illustrate firstElement() method.

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // create default vector of capacity 10
        Vector v = new Vector();
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL");
        // first element of vector
        System.out.println("1st element of vector is: " + v.firstElement());
    }
} 

Output:

1st element of vector is: Java

Example to illustrate get() methods

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        // create default vector of capacity 10
        Vector v = new Vector();
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL");
        // get the element at index 2
        System.out.println("Element at index 3 is: " + v.get(3));
    }
} 

Output:

Element at index 3 is: Oracle

Example to illustrate lastElement() method

import java.util.*;
class Demo {
    public static void main(String[] arg)
    {
        Vector v = new Vector(5);
// using add() to add elements in the vector
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL");
        // checking last element of vector
        System.out.println("vector's last element is: " + v.lastElement());
    }
} 

Output:

vector's last element is: SQL

Example to illustrate remove(int Index) method. 

import java.util.*;
class Demo {
    public static void main(String args[])
    {
        // creating an empty Vector
        Vector<String> v = new Vector<String>();
// Using add() to add elements in the Vector
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL"); 
        // Printing the Vector
        System.out.println("Vector: " + v);
        // Removing the element using remove()
        String rmv = v.remove(3);
        // Printing the removed element
        System.out.println("Removed element: " + rmv);
        // Printing the final Vector
        System.out.println("Final Vector: " + v);
    }
} 

Output:

Vector: [Java, Python, Android, Oracle, SQL]
Removed element: Oracle
Final Vector: [Java, Python, Android, SQL] 

Example to illustrate set() method.

import java.util.*;
class Demo {
    public static void main(String args[])
    {
        // creating an empty Vector
        Vector<String> v = new Vector<String>();
        // Using add() to add elements in the vector
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL"); 
        // Displaying the Vector
        System.out.println("Vector: " + v);
        // Using set() method to replace elements
        System.out.println("The Object that is replaced is: "
                           + v.set(2, "IOS"));
        // Using set() method to replace elements
        System.out.println("The Object that is replaced is: "
                           + v.set(4, "PL/Sql"));
        // Displaying the modified vector
        System.out.println("The new Vector is:" + v);
    }
} 

Output:

Vector: [Java, Python, Android, Oracle, SQL]
The Object that is replaced is: Android
The Object that is replaced is: SQL
The new Vector is:[Java, Python, IOS, Oracle, PL/Sql] 

Example to illustrate the elements() method .

import java.util.*;
class Demo {
    public static void main(String[] args)
    {
        // Creating Vector
        Vector<String> v = new Vector<String>(5);
        // Inserting elements into the table
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL"); 
        // printing the Vector
        System.out.println("The Vector is: " + v);
        // Creating an empty enumeration to store
        Enumeration enu = v.elements();
        System.out.println("The enumeration of values are:");
        // Displaying the Enumeration
        while (enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
} 

Output:

The enumeration of values are:
Java
Python
Android
Oracle
SQL 

Example to illustrate iterator()

import java.util.*;
class Demo {
public static void main(String args[])
    {
        // Creating a Vector
        Vector<String> v = new Vector<String>();
// Use add() to add elements into the Vector
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL"); 
        // Printing the Vector
        System.out.println("Vector: " + v);
        // Creating an iterator
        Iterator val = v.iterator();
        // Printing the values after iterating through the vector
        System.out.println("The iterator values are: ");
        while (val.hasNext()) {
            System.out.println(val.next());
        }
    }
} 

Output:

Vector: [Java, Python, Android, Oracle, SQL]
The iterator values are:
Java
Python
Android
Oracle
SQL 

Example to illustrate listIterator() method.

import java.util.*; 
class Demo {
    public static void main(String[] args)
    {
        // Declaring empty vector
        Vector<String> v = new Vector<String>();
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL"); 
        // Declaring list iterator
        ListIterator LItr = v.listIterator();
        // Forward iterations
        System.out.println("FWD>> Traversal:");
        while (LItr.hasNext()) {
            System.out.println(LItr.next());
        }
        // Backward iterations
        System.out.println("\n BWD<<< Traversal:");
        while (LItr.hasPrevious()) {
            System.out.println(LItr.previous());
        }
    }
} 

Output:

FWD>> Traversal:
Java
Python
Android
Oracle
SQL
 BWD<<< Traversal:
SQL
Oracle
Android
Python
Java 

Example to illustrate listIterator(int index) method.

import java.util.*;
class Demo{
    public static void main(String[] args)
    {
        // Declaring empty vector
        Vector<String> v = new Vector<String>();
        v.add("Java");
        v.add("Python");
        v.add("Android");
        v.add("Oracle");
        v.add("SQL"); 
        // Declaring list iterator
        ListIterator LItr = v.listIterator(1);
        // traversing
        while (LItr.hasNext()) {
            System.out.println(LItr.next());
        }
    }
} 

Output:

Python
Android
Oracle
SQL 

Example to illustrate subList(int fromIndex, int toIndex) method

import java.util.*;
 class Demo {
    public static void main(String[] args)
    {
        // Creating an empty Vector
        Vector<Integer> v = new Vector<Integer>();
        // Adding the elements using add()
        v.add(50);
        v.add(10);
        v.add(50);
        v.add(100);
        v.add(200);
        v.add(60);
        v.add(200);
        v.add(180);
        v.add(90);
        v.add(300);
        System.out.println("The Vector is: " + v);
        // Creating the sublist vector
        List<Integer> sub_list = new ArrayList<Integer>();
        // Setting the limits
        sub_list = v.subList(2, 5);
        //Printing the SubList data
        System.out.println("Values within the sub list: " + sub_list);
    }
} 

Output:

The Vector is: [50, 10, 50, 100, 200, 60, 200, 180, 90, 300]
Values within the sub list: [50, 100, 200] 

Example to illustrate toArray() method.

import java.util.*;
class Demo {
    public static void main(String args[])
    {
        // Creating a Vector
        Vector<String> v = new Vector<String>();
// Using add() to add elements into the Vector
        v.add("Welcome");
        v.add("To");
        v.add("Tutorials");
        v.add("And");
        v.add("Examples");
        // Printing the Vector
        System.out.println("The Vector: " + v);
        // Creating the array and using toArray() method
        Object[] ar = v.toArray();
        System.out.println("The array is:");
        for (int j = 0; j < ar.length; j++)
            System.out.println(ar[j]);
    }
} 

Output:

The Vector: [Welcome, To, Tutorials, And, Examples]
The array is:
Welcome
To
Tutorials
And
Examples 

Related Topics

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.