- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Vector copyInto() Method
Description
The Java Vector copyInto(Object[] anArray) method is used to copy the components of this vector into the specified array. The item at index k in this vector is copied into component k of the array.It means the position of elements are same in both the vector and array. The array must be big enough to hold all the objects in this vector otherwise an IndexOutOfBoundsException is thrown.
Declaration
Following is the declaration for java.util.Vector.copyInto() method
public void copyInto(Object[] anArray)
Parameters
anArray − This is the array into which the components get copied.
Return Value
Return type is void so does not return anything.
Exception
NullPointerException − if the given array is null.
Copying elements of Vector of Integers into an Array Example
The following example shows the usage of Java Vector copyInto() method to copy the components of this vector into the specified array. We're creating a vector object and an array of Integer. Then elements are added to vector and the array. Elements of the vector and array are printed before using copyinto() method. Then using copyInto() method, the elements of the vector are copied to array and updated array is printed again to validate the result.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<>(4); Integer anArray[] = new Integer[4]; anArray[0] = 100; anArray[1] = 100; anArray[2] = 100; anArray[3] = 100; // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(1); // elements in the array before copy System.out.println("Elements in the array before copy"); for (Integer number : anArray) { System.out.println("Element = " + number); } // copy into the array vec.copyInto(anArray); // elements in the array after copy System.out.println("Elements in the array after copy"); for (Integer number : anArray) { System.out.println("Element = " + number); } } }
Output
Let us compile and run the above program, this will produce the following result.
Elements in the array before copy Element = 100 Element = 100 Element = 100 Element = 100 Elements in the array after copy Element = 4 Element = 3 Element = 2 Element = 1
Copying elements of Vector of String into an Array Example
The following example shows the usage of Java Vector copyInto() method to copy the components of this vector into the specified array. We're creating a vector object and an array of String. Then elements are added to vector and the array. Elements of the vector and array are printed before using copyinto() method. Then using copyInto() method, the elements of the vector are copied to array and updated array is printed again to validate the result.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<String> vec = new Vector<>(4); String anArray[] = new String[4]; anArray[0] = "A"; anArray[1] = "A"; anArray[2] = "A"; anArray[3] = "A"; // use add() method to add elements in the vector vec.add("D"); vec.add("B"); vec.add("C"); vec.add("A"); // elements in the array before copy System.out.println("Elements in the array before copy"); for (String element : anArray) { System.out.println("Element = " + element); } // copy into the array vec.copyInto(anArray); // elements in the array after copy System.out.println("Elements in the array after copy"); for (String element : anArray) { System.out.println("Element = " + element); } } }
Output
Let us compile and run the above program, this will produce the following result.
Elements in the array before copy Element = A Element = A Element = A Element = A Elements in the array after copy Element = D Element = C Element = B Element = A
Copying elements of Vector of Object into an Array Example
The following example shows the usage of Java Vector copyInto() method to copy the components of this vector into the specified array. We're creating a vector object and an array of Student. Then elements are added to vector and the array. Elements of the vector and array are printed before using copyinto() method. Then using copyInto() method, the elements of the vector are copied to array and updated array is printed again to validate the result.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Student> vec = new Vector<>(4); Student anArray[] = new Student[4]; anArray[0] = new Student(1, "Julie"); anArray[1] = new Student(1, "Julie"); anArray[2] = new Student(1, "Julie"); anArray[3] = new Student(1, "Julie"); // use add() method to add elements in the vector vec.add(new Student(1, "Julie")); vec.add(new Student(2, "Robert")); vec.add(new Student(3, "Adam")); vec.add(new Student(4, "Jane")); // elements in the array before copy System.out.println("Elements in the array before copy"); for (Student element : anArray) { System.out.println("Element = " + element); } // copy into the array vec.copyInto(anArray); // elements in the array after copy System.out.println("Elements in the array after copy"); for (Student element : anArray) { System.out.println("Element = " + element); } } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
Output
Let us compile and run the above program, this will produce the following result.
Elements in the array before copy Element = [ 1, Julie ] Element = [ 1, Julie ] Element = [ 1, Julie ] Element = [ 1, Julie ] Elements in the array after copy Element = [ 1, Julie ] Element = [ 2, Robert ] Element = [ 3, Adam ] Element = [ 4, Jane ]
To Continue Learning Please Login
Login with Google