Thursday 8 May 2014

Vector : remove (int index) : Remove Element at Particular index

public synchronized E remove(int index)
Removes the element at the specified position in this Vector. Return element that was removed

import java.util.*;

class VectorRemove{
 public static void main(String args[]){
  Vector<Integer> myVector;
  ListIterator<Integer> myIter;
  
  myVector = new Vector<> ();
  
  /*Add Elements to myVector*/
  myVector.add(10);
  myVector.add(11);
  myVector.add(12);
  myVector.add(13);
  myVector.add(14);

  System.out.println("Elements in myVector are ");
  System.out.println(myVector);
  
  System.out.println("\nRemoving element at index 1");
  myVector.remove(1);
  
  System.out.println("\nElements in myVector are ");
  System.out.println(myVector);
 }
}

Output
Elements in myVector are
[10, 11, 12, 13, 14]

Removing element at index 1

Elements in myVector are
[10, 12, 13, 14]


1. Throws ArrayIndexOutOfBoundsException if the index is out of range

import java.util.*;

class VectorRemoveIndexOut{
 public static void main(String args[]){
  Vector<Integer> myVector;
  ListIterator<Integer> myIter;
  
  myVector = new Vector<> ();
  
  /*Add Elements to myVector*/
  myVector.add(10);
  myVector.add(11);
  myVector.add(12);
  myVector.add(13);
  myVector.add(14);

  System.out.println("Elements in myVector are ");
  System.out.println(myVector);
  
  System.out.println("\nRemoving element at index 6");
  myVector.remove(6);
 }
}

Output
Elements in myVector are
[10, 11, 12, 13, 14]

Removing element at index 6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index
 out of range: 6
        at java.util.Vector.remove(Vector.java:827)
        at VectorRemoveIndexOut.main(VectorRemoveIndexOut.java:21)





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment