Vector is the pre-defined class provided by java for making heterogeneous list. Java introduce vector in ver5 and later. It is define under java.util package. Vector is like the dynamic array.

Methods of vector

addElement() Method

This adds a new value of any type in vector list.

Syntax

obj.addElement(value);

Example of addElement() Method in Java

Input Value

import java.util.Vector;  
    public class vectorg {    
    public static void main(String arg[]) {  
                
          Vector<Integer> a = new Vector<>(3);  
          a.add(25);  
          a.add(-35);  
          a.add(45);          
          System.out.println("Vector: "+a);            
          a.addElement(-65);   
          System.out.println("after vector: "+a);             
            }  
 
}
addElement() Method in Java

Let’s Run the code

Input Values

Output Value

Output Value

size() Method

This returns the size of the vector means how many elements in vector list

Syntax

obj.size(); 

Example of size() Method in Java

Input Value

import java.util.*;

public class vectorg {
	public static void main(String args[])
	{
		Vector<String> a = new Vector<String>();

		a.add("hello");
		a.add("basic");
		a.add("student");
		a.add("engineer");
		
		System.out.println("Vector: " + a);
		System.out.println("The size is: " + a.size());
	}
}
size() Method in Java

Let’s Run the code

Input Values

Output Value

Output Value

elementAt() Method

This return the given index position value of the vector list.

Syntax

obj.element(index);

Example of elementAt() Method in Java

Input Value

import java.util.*;

public class vectorg {
	public static void main(String args[])
	{
		Vector < String > colors = new Vector < String > ();  
          
          colors.add("Red");  
          colors.add("Blue");  
          colors.add("Yellow");  
          colors.add("gray");  
           
          System.out.println("Element position = " +colors.elementAt(1));  
          System.out.println("Element position = " +colors.elementAt(3));
	}
}
elementAt() Method in Java

Let’s Run the code

Input Values

Output Value

Output Value

insertElementAt() Method

This methods inserts the new item at specified position in vector list

Syntax

insertElementAt(item,pos);

Example of insertElementAt() Method in Java

Input Value

import java.util.*;

public class vectorg {
	public static void main(String args[])
	{  
        Vector<Integer> a = new Vector<>();   
        a.add(5);  
        a.add(10);  
        a.add(15);          
        a.add(20);  
        a.add(25);  
          
        System.out.println("before = "+a);    
        a.insertElementAt(40, 4);  
        System.out.println("after= "+a); 
	}
}
insertElementAt() Method in Java

Let’s Run the code

Input Values

Output Value

Output Value

removeElement() Method

This removes the given item element from Vector list.

Syntax

obj.removeElement(item);

Example of removeElement() Method in Java

Input Value

import java.util.*;

public class vectorg {
	public static void main(String args[])
	{          
               
        Vector < Integer > a = new Vector < > ();  
        a.add(5);  
        a.add(10);  
        a.add(15);  
        a.add(20);  
        a.add(25);  
        System.out.println("total Values: " +a);  
        System.out.println("Remove: "+a.remove((Integer)15));  
        System.out.println("vector: " +a);   
	}
}
removeElement() Method in Java

Let’s Run the code

Input Values

Output Value

Output Value

removeElementAt() Method

This removes the given position item from vector list.

Syntax

obj.removeElementAt(pos);

Example of removeElementAt() Method in Java

Input Value

import java.util.*;

public class vectorg {
	public static void main(String args[])
	{               
        Vector<String> a = new Vector<String>();
  
        a.add("5");
        a.add("10");
        a.add("15");
        a.add("20");
        a.add("25");
  
        System.out.println("Vector: " + a);
        System.out.println("size : " + a.size());

        a.removeElementAt(4);
        System.out.println("Final: " + a);
        System.out.println("size: " + a.size());  
	}
}
removeElementAt() Method in Java

Let’s Run the code

Input Values

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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