Saturday, November 23, 2024
Google search engine
HomeLanguagesJavaVector copyInto() Method in Java

Vector copyInto() Method in Java

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




// Java code to illustrate copyInto()
import java.util.*;
 
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
 
        // Use add() method to add elements into the Vector
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
 
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
 
        // Creating an array
        String arr[] = new String[6];
 
        arr[0] = "Hello";
        arr[1] = "World";
 
        // Displaying the initial array
        System.out.println("The initial array is: ");
        for (String str : arr)
            System.out.println(str);
 
        // Copying
        vec_tor.copyInto(arr);
 
        // The final array
        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




// Java code to illustrate copyInto()
import java.util.*;
 
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>();
 
        // Use add() method to add elements into the Vector
        vec_tor.add(10);
        vec_tor.add(20);
        vec_tor.add(30);
        vec_tor.add(40);
        vec_tor.add(50);
 
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
 
        // Creating an array
        Integer arr[] = new Integer[6];
 
        arr[0] = 50;
        arr[1] = 60;
        arr[2] = 70;
        arr[3] = 80;
        arr[4] = 90;
 
        // Displaying the initial array
        System.out.println("The initial array is: ");
        for (Integer str : arr)
            System.out.println(str);
 
        // Copying
        vec_tor.copyInto(arr);
 
        // The final array
        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)

RELATED ARTICLES

Most Popular

Recent Comments