Write a Java program to convert a Vector collection into an Object array


The code you provided is written in Java and demonstrates how to convert a Vector collection to an array using the toArray() method. In the code, a Vector named vec_list is created and populated with integer values.

The toArray() method is then called on the vec_list object without passing any arguments. This method returns an array containing all the elements of the vector. The resulting array is stored in an Object[] variable named arr. Finally, the original vector collection and the elements of the array are printed using System.out.println() and System.out.print() statements.

Source Code

import java.util.*;
public class Object_Array
{
	public static void main(String[] args)
	{
		Vector < Integer > vec_list = new Vector < Integer > ();
		vec_list.add(100);
		vec_list.add(20);
		vec_list.add(57);
		vec_list.add(40);
		vec_list.add(5);
		vec_list.add(60);
		vec_list.add(7);
		vec_list.add(66	);
		vec_list.add(35);
		vec_list.add(10);
 
		Object[] arr = vec_list.toArray();
 
		System.out.println("Vector Collection : " + vec_list);
 
		System.out.println("Array Elements : ");
		for (Object item: arr)
		{
			System.out.print(item + " ");
		}
	}
}

Output

Vector Collection : [100, 20, 57, 40, 5, 60, 7, 66, 35, 10]
Array Elements :
100 20 57 40 5 60 7 66 35 10

Example Programs