Open In App

Removing Element from Specified Index in Java Vector

Last Updated : 28 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Element at the specified index in Java vector can be removed using remove() method. this method is present in java.util.Vector class. We can pass index of the element which will be removed as an argument. remove() method return method returns the element that was removed.

Syntax : 

public removed_element remove(int index)

Parameters: The index of the element to be removed.

Return Type: This method returns the element that was removed from the list.

Exception: This method throws IndexOutOfBoundsException if the index is out of range.

Code:

Java




// Java program for removing element
// at specified index
  
import java.util.Vector;
public class GFG {
    public static void main(String arg[])
    {
        // Create an Vector
        Vector<Integer> vector = new Vector<>();
        
        // Add elements in the vector
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(20);
        vector.add(40);
  
        // display original vector
        System.out.println("Values in vector: " + vector);
  
        // remove 2 index element and store the value in r
        int r = vector.remove((2));
  
        // display removed element
        System.out.println("Removed element: " + r);
  
        // display vector after 2 index element
        System.out.println("Values in vector: " + vector);
    }
}


Output

Values in vector: [10, 20, 30, 20, 40]
Removed element: 30
Values in vector: [10, 20, 20, 40]

Exception in remove() method:

When we will try to remove an element at the index which is greater than or equal to the size of the array list, the editor will give us the IndexOutOfBound Exception on running.

Java




// Java program to show the exception 
// of remove() method 
    
import java.util.ArrayList; 
    
public class ArrayListDemo { 
   public static void main(String[] args) { 
          
      // create an empty array list with an initial capacity 
      ArrayList<Integer> arrlist = new ArrayList<Integer>(5); 
    
      // use add() method to add elements in the deque 
      arrlist.add(26); 
      arrlist.add(18); 
      arrlist.add(31); 
      arrlist.add(42); 
    
      System.out.println("Size of list: " + arrlist.size()); 
          
      // let us print all the elements available in list 
      for (Integer number : arrlist) { 
         System.out.println("Number = " + number); 
      }   
          
      // Removes element at 5th position 
      // which is not present in the list 
      // and will therefore give IndexOutOfBound exception 
      arrlist.remove(4); 
    
      System.out.println("Now, Size of list: " + arrlist.size()); 
          
      // let us print all the elements available in list 
      for (Integer number : arrlist) { 
         System.out.println("Number = " + number); 
      
   
}


Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads