public
synchronized E set(int index, E element)
Replaces
the element at the specified position in this Vector with the
specified element. Return the element previously at the specified
position
import java.util.*; class VectorSet{ 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(13); System.out.println("Elements in myVector "); System.out.println(myVector); System.out.println("\nReplacing value at index 1 to 0"); myVector.set(1, 0); System.out.println("\nElements in myVector "); System.out.println(myVector); } }
Output
Elements in myVector [10, 11, 12, 13] Replacing value at index 1 to 0 Elements in myVector [10, 0, 12, 13]
1. Throws
ArrayIndexOutOfBoundsException if the index is out of range
import java.util.*; class VectorSetIndexOut{ 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(13); System.out.println("Elements in myVector "); System.out.println(myVector); System.out.println("\nReplacing value at index 1 to 0"); myVector.set(6, 0); System.out.println("\nElements in myVector "); System.out.println(myVector); } }
Output
Elements in myVector [10, 11, 12, 13] Replacing value at index 1 to 0 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 6 at java.util.Vector.set(Vector.java:762) at VectorSetIndexOut.main(VectorSetIndexOut.java:18)
No comments:
Post a Comment