Vector Class In Java | Methods Of Vector Class | PLM Developer

Vector is a resizable array in java. Unlike arrays, there is no size limit for Vector. Vector adjusts it's capacity automatically while adding or removing the objects at runtime. Hence it is known as a dynamic array. Vector is a class that extends AbstractList and implements List interface.

The behavior of Vector:

  1. Vector maintains insertion order but unlike ArrayList, It is synchronized so it gives poor performance while adding, deleting, searching of elements.
  2. The default capacity of the Vector is 10.
  3. Vector allows us to store heterogeneous values.
  4. We cannot use Vector for primitive data types.

ArrayList constructors:

//Constructor to initilize empty Vector

Vector list = new Vector();

//Constructor to initilize Vector with capacity

Vector list = new Vector(int capacity);

//Constructor to initilize Vector with elements from collection

Vector list = new Vector(int initialCapacity, int capacityIncrement);

Methods of Vector:

Add elements to Vector:

by using add() method we can add single elements to Vector

import java.util.ArrayList;

public class ArrayListImpl {

    public static void main(String[] args) {

        //create List with ArrayList class

        ArrayList<String> numbersList = new ArrayList<>();

        //Ading elements to list





        //Printing the elements in ArrayList

        System.out.println("List values: "+numbersList);




List values: [One, Two, Three, Four]

Add elements of ArrayList to another ArrayList:>

By using the addAll() method we can add all elements of an array list to another array list.

import java.util.Vector;

public class VectorImpl {

    public static void main(String[] args) {

               //create Vector class
               Vector numbersList1 = new Vector<>();

               //Ading elements to list

               //Access elements using add method
               String str = numbersList1.get(0);

               //Printing the element from numbersList1
               System.out.println("Value is: "+str);

               //Printing the elements in numbersList1
               System.out.println("List values: "+numbersList1);

               Vector numbersList2 = new Vector<>();

               //Adding all elements of numbersList2 to numbersList1
               //Printing the elements in numbersList2 after adding numbersList1
               System.out.println("List values: "+numbersList2);


Value is: One

List values: [One, Two, Three, Four]

List values: [Five, Six, One, Two, Three, Four]

Post a Comment