Last Updated : 11 Jul, 2025
In Java, the copyInto() method is used to copy all the components from one vector to another array, having enough space to hold all of the components of the vector. It is to be noted that the index of the elements remains unchanged. The elements present in the array are replaced by the elements of the vector starting from index 0.
Example: Here, we use the copyInto() method to copy all the elements from a Vector into an array of type Integer.
Java
// Java program to demonstrate the
// use of copyInto() with Integer type
import java.util.Vector;
class Geeks {
public static void main(String[] args) {
// Create a Vector and
// add some elements
Vector<Integer> v = new Vector<>();
v.add(1);
v.add(2);
v.add(3);
// Display the Vector elements
System.out.println("Vector: " + v);
// Create an array with
// some initial elements
Integer[] arr = new Integer[] { 10, 20, 30 };
// Display the initial state of the array
System.out.print("The initial array is: ");
for (Integer i : arr) {
System.out.print(i + " ");
}
System.out.println();
// Use the copyInto() method to copy
// the elements from the Vector to the array
v.copyInto(arr);
// Display the final array after copying
System.out.print("The final array is: ");
for (Integer i : arr) {
System.out.print(i + " ");
}
}
}
Vector: [1, 2, 3] The initial array is: 10 20 30 The final array is: 1 2 3Syntax of Vector copyInto() Method
public void copyInto(Object[] array )
Parameter: The parameter array[] is of the type vector. This is the array into which the elements of the vector are to be copied.
Exceptions:
Example 2: Here, we use the copyInto() method to copy all the elements from a Vector into an array of type String.
Java
// Java program to demonstrate the
// use of copyInto() with String type
import java.util.Vector;
class Geeks {
public static void main(String[] args) {
// Create a Vector and
// add some elements
Vector<String> v = new Vector<>();
v.add("Geeks");
v.add("For");
v.add("Geeks");
// Display the Vector elements
System.out.println("Vector: " + v);
// Create an array with
// some initial elements
String[] arr = new String[] { "Hello", "World", "Java" };
// Display the initial state of the array
System.out.print("The initial array is: ");
for (String i : arr) {
System.out.print(i + " ");
}
System.out.println();
// Use the copyInto() method to copy
// the elements from the Vector to the array
v.copyInto(arr);
// Display the final array after copying
System.out.print("The final array is: ");
for (String i : arr) {
System.out.print(i + " ");
}
}
}
Vector: [Geeks, For, Geeks] The initial array is: Hello World Java The final array is: Geeks For Geeks
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4