Home » Java programming language

Java Vector copyInto() Method with Example

Vector Class copyInto() method: Here, we are going to learn about the copyInto() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 15, 2020

Vector Class copyInto() method

  • copyInto() method is available in java.util package.
  • copyInto() method is used to copy all of the elements that exist in this Vector and paste it into the given object array (arr).
  • copyInto() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • copyInto() method does not throw an exception at the time of copying elements.

Syntax:

    public void copyInto(Object[] arr);

Parameter(s):

  • Object[] arr – represents the container of copied elements.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void copyInto(Object[] arr) method 
// of Vector 

import java.util.*;

public class CopyIntoOfVector {
    public static void main(String[] args) {
        // Instantiates a Vector object  with
        // initial capacity of "10"
        Vector < String > v = new Vector < String > (10);
        String str[] = new String[5];

        // By using add() method is to add the
        // elements in this v
        v.add("C");
        v.add("C++");
        v.add("JAVA");

        // Display Vector
        System.out.println("v: " + v);

        System.out.println("str: ");
        for (String s: str)
            System.out.println("str[]: " + s);

        // By using copyInto() method is to copy 
        // all of the elements of this vector v
        // in to a string array str[]
        v.copyInto(str);

        System.out.println("v.copyInto(str): ");
        for (String s: str)
            System.out.println("str[]: " + s);
    }
}

Output

v: [C, C++, JAVA]
str: 
str[]: null
str[]: null
str[]: null
str[]: null
str[]: null
v.copyInto(str): 
str[]: C
str[]: C++
str[]: JAVA
str[]: null
str[]: null


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.