Saturday , August 19 2017
Home / java / Vector in Java

Vector in Java

Vector is a class in Java, which is one of the implementation class of java.util.List interface like ArrayList.

Vector in Java :

Like ArrayList, Vector is also ordered collection. It maintains the insertion order. Vector class is mainly useful for multi threaded environment, since all methods implemented in Vector class is thread safe that is all are synchronized methods.

The constructors for creating a Vector instance are:

  • Vector():Creates a Vector instance of default capacity of 10.

  • Vector(int capacity):Creates a Vector instance with the specified capacity.

  • Vector(int capacity, int incr):Creates a Vector instance with the specified capacity and increment value. The increment specifies the number of elements to be allocated for every resize operation on the vector.

  • Vector(Collection<? extends E>c):Creates a Vector instance and populates it with the elements in the collection c.

Usage of Vector in Java


package com.onlinetutorialspoint;

import java.util.Vector;

public class VectorCapacity {

    public static void main(String[] args) {

        // creating Vector with Default constructor
        Vector vec1 = new Vector();
        System.out.println("initial Capacity : " + vec1.capacity());

        // Creating Vector with initial capacity 5
        Vector vec2 = new Vector(5);
        System.out.println("vec2 initial capacity : " + vec2.capacity());

        //Adding the elements to Vector
        vec2.add(10);
        vec2.add(30);
        vec2.add(60);
        vec2.add(40);
        vec2.add(80);
        vec2.add(100);

        System.out.println("vec2 size : " + vec2.size());

        // If we doesn't mention the increment value, by default the vector incremets double of the capacity.
        System.out.println("vec2 Capacity after resize: " + vec2.capacity());

        // Creating the Vector with initial capacity and increment value.
        Vector vec3 = new Vector(3, 2);
        System.out.println("vec3 initial capacity : " + vec3.capacity());
        vec3.add(40);
        vec3.add(80);
        vec3.add(100);
        System.out.println("vec3 size : " + vec3.size());
        vec3.add(500);
        System.out.println("vec3 capacity after resize: " + vec3.capacity());

    }
}
initial Capacity : 10
vec2 initial capacity : 5
vec2 size : 6
vec2 Capacity after resize: 10
vec3 initial capacity : 3
vec3 size : 3
vec3 capacity after resize: 5
[/box

On the above example, we have created 3 different Vector objects with different constructors.

vec1 Object is created by using the default constructor, while using the default constructor, the Vector will be created with the default values (initial capacity= 10 and increment value= “Double of the initial capacity”).

vec2 Object is created by using the initial capacity of 5 and default increment value.

vec3 Object is created by using the initial capacity of 3 and increment value is 2.Methods in the Vector in Java

You can access all List interface methods in the Vector class and use them to perform operations on vector. In addition, you can also use the legacy methods in the Vector class, such as addElement(), elementAt(), contains(), insertElementAt(), and setElementAt().

The most commonly used methods in the Vector class are:

  • void add(int index, E obj):Inserts the element obj at the location specified by index. Replaces the existing element at that location.

  • E get(int index):Returns the element at the location specified by index.

  • E set(int index, E obj):Assigns the element obj to the specified location and returns the old element at that location.

  • boolean remove(int index):Removes the element at the specified location.

  • <T> T[] toArray(T[] a):Returns an array of type T that contains all the elements of the ArrayList instance a in the same sequence.

Example For Vector Operations:


package com.onlinetutorials;
import java.util.Vector;

public class VectorOperationsDemo {

    public static void main(String[] args) {

        Vector vec1 = new Vector();

        // Adding Elements to Vector
        
        vec1.add(100);
        vec1.add(200);
        vec1.add(300);
        vec1.add(100);
        vec1.add(500);
        vec1.add(200);

        System.out.println("vec1 : " + vec1);

        // Add element to Vector at specific index position
        
        vec1.insertElementAt(10, 1);
        vec1.add(7, 900);

        System.out.println("vec1 : " + vec1);
        
        // Getting the element from Vector using the index position

        System.out.println("Vector's 4th Position element " + vec1.elementAt(4));
        System.out.println("Vector's 4th Position element " + vec1.get(5));
        
        // Check whether the element exist in Vector ot not
        
        System.out.println("Vectors is contains 900 ? " + vec1.contains(900));

        // Update a specific element in Vector with index position
        
        vec1.setElementAt(1000, 0);
        vec1.set(3, 2000);

        System.out.println("vec1 : " + vec1);
        
        // converting the Vector to Array
        
        Object[] toArray = vec1.toArray();
        System.out.println("toArray : "+toArray[0]);
        
    }
}

vec1 : [100, 200, 300, 100, 500, 200] vec1 : [100, 10, 200, 300, 100, 500, 200, 900] Vector’s 4th Position element 100
Vector’s 4th Position element 500
Vector’s is contains true
vec1 : [1000, 10, 200, 2000, 100, 500, 200, 900] toArray : 1000
[/box

 On the above example, we have done the basic CRUD (Create Read Update Delete) operations on Vector.

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *