This example demonstrates how to convert a Vector
object into an array. You can use the Vector.copyInto(Object[] array)
method to create a copy of the Vector
object in array form.
package org.kodejava.util;
import java.util.Vector;
public class VectorToArray {
public static void main(String[] args) {
Vector<Integer> vector = new Vector<>();
vector.add(10);
vector.add(20);
vector.add(30);
vector.add(40);
vector.add(50);
// Declares and initializes an Integer array.
Integer[] numbers = new Integer[vector.size()];
// Copies the components of this vector into the specified
// array of Integer
vector.copyInto(numbers);
for (Integer number : numbers) {
System.out.println("Number: " + number);
}
}
}
The result of the code snippet above:
Number: 10
Number: 20
Number: 30
Number: 40
Number: 50
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024