The java.util.vector.copyInto() method is used to copy all of the components from this 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.
Syntax:
Vector.copyInto(Object array[])
Parameters: The parameter array[] is of the type of vector. This is the array into which the elements of the vector are to be copied.
Return Value: The method is of void type and does not return any values. Exception: The method throws NullPointerException if the array is NULL. Below programs illustrates the Java.util.Vector.copyInto() method:
Program 1:
Java
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
Vector<String> vec_tor = new Vector<String>();
vec_tor.add("Welcome");
vec_tor.add("To");
vec_tor.add("Geeks");
vec_tor.add("4");
vec_tor.add("Geeks");
System.out.println("Vector: " + vec_tor);
String arr[] = new String[6];
arr[0] = "Hello";
arr[1] = "World";
System.out.println("The initial array is: ");
for (String str : arr)
System.out.println(str);
vec_tor.copyInto(arr);
System.out.println("The final array is: ");
for (String str : arr)
System.out.println(str);
}
}
|
Output:
Vector: [Welcome, To, Geeks, 4, Geeks]
The initial array is:
Hello
World
null
null
null
null
The final array is:
Welcome
To
Geeks
4
Geeks
null
Program 2:
Java
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
Vector<Integer> vec_tor = new Vector<Integer>();
vec_tor.add(10);
vec_tor.add(20);
vec_tor.add(30);
vec_tor.add(40);
vec_tor.add(50);
System.out.println("Vector: " + vec_tor);
Integer arr[] = new Integer[6];
arr[0] = 50;
arr[1] = 60;
arr[2] = 70;
arr[3] = 80;
arr[4] = 90;
System.out.println("The initial array is: ");
for (Integer str : arr)
System.out.println(str);
vec_tor.copyInto(arr);
System.out.println("The final array is: ");
for (Integer str : arr)
System.out.println(str);
}
}
|
Output:
Vector: [10, 20, 30, 40, 50]
The initial array is:
50
60
70
80
90
null
The final array is:
10
20
30
40
50
null
Time complexity: O(n), // n is the size of the array.
Auxiliary space: O(n)
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
24 May, 2023
Like Article
Save Article