public
synchronized void copyInto(Object[] anArray)
Copies
the components of this vector into the specified array.
import java.util.*; class VectorCopyInto{ public static void main(String args[]){ Vector<Integer> myVector; Object myArray[]; myVector = new Vector<> (); /* Add Elements to myVector */ for(int i=1; i<6; i++) myVector.add(i); int size = myVector.size(); myArray = new Object[size]; System.out.println("Elements in myVector are"); System.out.println(myVector); myVector.copyInto(myArray); System.out.println("Elements in myArray are"); for(Object obj:myArray) System.out.print(obj + " "); } }
Output
Elements in myVector are [1, 2, 3, 4, 5] Elements in myArray are 1 2 3 4 5
1. Throws
NullPointerException if the given array is null
import java.util.*; class VectorCopyIntoNullPointer{ public static void main(String args[]){ Vector<Integer> myVector = new Vector<> (); Object myArray[] = null; myVector.copyInto(myArray); } }
Output
Exception in thread "main" java.lang.NullPointerException at java.lang.System.arraycopy(Native Method) at java.util.Vector.copyInto(Vector.java:188) at VectorCopyIntoNullPointer.main(VectorCopyIntoNullPointer.java:8)
2. Throws
IndexOutOfBoundsException if the specified array is not large enough
to hold all the components of this vector
import java.util.*; class VectorCopyIntoIndexOut{ public static void main(String args[]){ Vector<Integer> myVector; Object myArray[] = new Object[3]; myVector = new Vector<> (); /* Add Elements to myVector */ for(int i=1; i<6; i++) myVector.add(i); myVector.copyInto(myArray); } }
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at java.util.Vector.copyInto(Vector.java:188) at VectorCopyIntoIndexOut.main(VectorCopyIntoIndexOut.java:14)
3.
Throws ArrayStoreException if a component of this vector is not of a
runtime type that can be stored in the specified array
import java.util.*; class VectorCopyIntoArrayStore{ public static void main(String args[]){ Vector<String> myVector; Integer myArray[] = new Integer[6]; myVector = new Vector<> (); /* Add Elements to myVector */ for(int i=1; i<6; i++) myVector.add("i"); myVector.copyInto(myArray); } }
Output
Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.Vector.copyInto(Vector.java:188) at VectorCopyIntoArrayStore.main(VectorCopyIntoArrayStore.java:14)
No comments:
Post a Comment