Java Utility Library

Java Vector - copyInto() Method



The java.util.Vector.copyInto() method is used to copy the components of the vector into the specified array. The item at index k in the vector is copied into component k of the array.

Syntax

public void copyInto(Object[] anArray)

Parameters

anArray Specify the array into which the components get copied.

Return Value

void type.

Exception

  • Throws NullPointerException, if the given array is null
  • Throws IndexOutOfBoundsException, if the specified array is not large enough to hold all the components of the vector
  • Throws ArrayStoreException, if a component of the vector is not of a runtime type that can be stored in the specified array

Example:

In the example below, the java.util.Vector.copyInto() method is used to copy a vector into the specified array.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);
    
    Integer Arr[] = {0, 0, 0};
    //printing array
    System.out.print("Before copyInto method, Arr contains: ");
    for(Integer i: Arr)
      System.out.print(i +" ");

    MyVector.copyInto(Arr);

    //printing array
    System.out.print("\nAfter copyInto method, Arr contains: ");
    for(Integer i: Arr)
      System.out.print(i +" ");
  }
}

The output of the above code will be:

Before copyInto method, Arr contains: 0 0 0 
After copyInto method, Arr contains: 10 20 30 

❮ Java.util - Vector