java.util.Vector.copyInto() 方法
描述
copyInto(Object[] anArray) 方法用于将此向量的分量复制到指定的数组中。 该向量中索引k处的项被复制到数组的分量k中。这意味着元素在向量和数组中的位置相同。 数组必须足够大以容纳此向量中的所有对象,否则会引发 IndexOutOfBoundsException。
声明
以下是 java.util.Vector.copyInto() 方法的声明
public void copyInto(Object[] anArray)
参数
anArray − 这是组件被复制到的数组。
返回值
返回类型是 void 所以不返回任何东西。
异常
NullPointerException − 如果给定的数组为空。
示例
下面的例子展示了 java.util.Vector.copyInto() 方法的使用。
package com.tutorialspoint;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec with an initial capacity of 4
Vector<Integer> vec = new Vector<Integer>(4);
Integer anArray[] = new Integer[4];
anArray[0] = 100;
anArray[1] = 100;
anArray[2] = 100;
anArray[3] = 100;
// use add() method to add elements in the vector
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
// numbers in the array before copy
System.out.println("Numbers in the array before copy");
for (Integer number : anArray) {
System.out.println("Number = " + number);
}
// copy into the array
vec.copyInto(anArray);
// numbers in the array after copy
System.out.println("Numbers in the array after copy");
for (Integer number : anArray) {
System.out.println("Number = " + number);
}
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Numbers in the array before copy Number = 100 Number = 100 Number = 100 Number = 100 Numbers in the array after copy Number = 4 Number = 3 Number = 2 Number = 1

