public
synchronized void removeElementAt(int index)
Deletes
the component at the specified index. After deletion size of the
vector is decreased by 1.
import java.util.*; class VectorRemoveElementAt{ public static void main(String args[]){ Vector<Integer> myVector; myVector = new Vector<> (); /*Add Elements to myVector*/ myVector.add(10); myVector.add(11); myVector.add(12); myVector.add(10); myVector.add(10); System.out.println("Elements in myVector "); System.out.println(myVector); System.out.println("\nRemove the element at index 0"); myVector.removeElementAt(0); System.out.println("Elements in myVector "); System.out.println(myVector); } }
Output
Elements in myVector [10, 11, 12, 10, 10] Remove the element at index 0 Elements in myVector [11, 12, 10, 10]
1. Throws
ArrayIndexOutOfBoundsException if the index is out of range
import java.util.*; class VectorRemoveElementAtIndexOut{ public static void main(String args[]){ Vector<Integer> myVector; myVector = new Vector<> (); /*Add Elements to myVector*/ myVector.add(10); myVector.add(11); myVector.add(12); myVector.add(10); myVector.add(10); System.out.println("Elements in myVector "); System.out.println(myVector); System.out.println("\nRemove the element at index 6"); myVector.removeElementAt(6); System.out.println("Elements in myVector "); System.out.println(myVector); } }
Output
Elements in myVector [10, 11, 12, 10, 10] Remove the element at index 6 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 >= 5 at java.util.Vector.removeElementAt(Vector.java:554) at VectorRemoveElementAtIndexOut.main(VectorRemoveElementAtIndexOut.java :20)
No comments:
Post a Comment