Vector toArray() Method in Java with Examples (original) (raw)

Last Updated : 24 May, 2023

1. toArray()

The toArray() method of Vector class in Java is used to form an array of the same elements as that of the Vector. Basically, it copies all the element from a Vector to a new array.

Syntax:

Object[] arr = Vector.toArray()

Parameters: The method does not take any parameters.

Return Value: The method returns an array containing elements similar to the Vector.

Below programs illustrate the Vector.toArray() method:

Program 1:

Java

import java.util.*;

public class VectorDemo {

`` public static void main(String args[])

`` {

`` Vector<String> vec_tor = new Vector<String>();

`` vec_tor.add("Welcome");

`` vec_tor.add("To");

`` vec_tor.add("Geeks");

`` vec_tor.add("For");

`` vec_tor.add("Geeks");

`` System.out.println("The Vector: " + vec_tor);

`` Object[] arr = vec_tor.toArray();

`` System.out.println("The array is:");

`` for ( int j = 0 ; j < arr.length; j++)

`` System.out.println(arr[j]);

`` }

}

Output:

The Vector: [Welcome, To, Geeks, For, Geeks] The array is: Welcome To Geeks For Geeks

Program 2:

Java

import java.util.*;

public class VectorDemo {

`` public static void main(String args[])

`` {

`` Vector<Integer> vec_tor = new Vector<Integer>();

`` vec_tor.add( 10 );

`` vec_tor.add( 15 );

`` vec_tor.add( 30 );

`` vec_tor.add( 20 );

`` vec_tor.add( 5 );

`` vec_tor.add( 25 );

`` System.out.println("The Vector: " + vec_tor);

`` Object[] arr = vec_tor.toArray();

`` System.out.println("The array is:");

`` for ( int j = 0 ; j < arr.length; j++)

`` System.out.println(arr[j]);

`` }

}

Output:

The Vector: [10, 15, 30, 20, 5, 25] The array is: 10 15 30 20 5 25

2. toArray(arr[])

The toArray(arr[]) method of Vector class in Java is used to form an array of the same elements as that of the Vector. It returns an array containing all of the elements in this Vector in the correct order; the run-time type of the returned array is that of the specified array. If the Vector fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this Vector. If the Vector fits in the specified array with room to spare (i.e., the array has more elements than the Vector), the element in the array immediately following the end of the Vector is set to null. (This is useful in determining the length of the Vector only if the caller knows that the Vector does not contain any null elements.)

Syntax:

Object[] arr1 = Vector.toArray(arr[])

Parameters: The method accepts one parameter arr[] which is the array into which the elements of the Vector are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Return Value: The method returns an array containing the elements similar to the Vector.

Exception: The method might throw two types of exception:

Below program illustrates the working of the Vector.toArray(arr[]) method.

Program 1: When array is of the size of Vector

Java

import java.util.*;

public class VectorDemo {

`` public static void main(String args[])

`` {

`` Vector<String> vec_tor = new Vector<String>();

`` vec_tor.add("Welcome");

`` vec_tor.add("To");

`` vec_tor.add("Geeks");

`` vec_tor.add("For");

`` vec_tor.add("Geeks");

`` System.out.println("The Vector: " + vec_tor);

`` String[] arr = new String[ 5 ];

`` arr = vec_tor.toArray(arr);

`` System.out.println("The arr[] is:");

`` for ( int j = 0 ; j < arr.length; j++)

`` System.out.println(arr[j]);

`` }

}

Output:

The Vector: [Welcome, To, Geeks, For, Geeks] The arr[] is: Welcome To Geeks For Geeks

Program 2: When array is less than the size of Vector

Java

import java.util.*;

public class VectorDemo {

`` public static void main(String args[])

`` {

`` Vector<String> vec_tor = new Vector<String>();

`` vec_tor.add("Welcome");

`` vec_tor.add("To");

`` vec_tor.add("Geeks");

`` vec_tor.add("For");

`` vec_tor.add("Geeks");

`` System.out.println("The Vector: " + vec_tor);

`` String[] arr = new String[ 1 ];

`` arr = vec_tor.toArray(arr);

`` System.out.println("The arr[] is:");

`` for ( int j = 0 ; j < arr.length; j++)

`` System.out.println(arr[j]);

`` }

}

Output:

The Vector: [Welcome, To, Geeks, For, Geeks] The arr[] is: Welcome To Geeks For Geeks

Program 3: When array is more than the size of Vector

Java

import java.util.*;

public class VectorDemo {

`` public static void main(String args[])

`` {

`` Vector<String> vec_tor = new Vector<String>();

`` vec_tor.add("Welcome");

`` vec_tor.add("To");

`` vec_tor.add("Geeks");

`` vec_tor.add("For");

`` vec_tor.add("Geeks");

`` System.out.println("The Vector: " + vec_tor);

`` String[] arr = new String[ 10 ];

`` arr = vec_tor.toArray(arr);

`` System.out.println("The arr[] is:");

`` for ( int j = 0 ; j < arr.length; j++)

`` System.out.println(arr[j]);

`` }

}

Output:

The Vector: [Welcome, To, Geeks, For, Geeks] The arr[] is: Welcome To Geeks For Geeks null null null null null

Program 4: To demonstrate NullPointerException

Java

import java.util.*;

public class VectorDemo {

`` public static void main(String args[])

`` {

`` Vector<String> vec_tor = new Vector<String>();

`` vec_tor.add("Welcome");

`` vec_tor.add("To");

`` vec_tor.add("Geeks");

`` vec_tor.add("For");

`` vec_tor.add("Geeks");

`` System.out.println("The Vector: " + vec_tor);

`` try {

`` String[] arr = null ;

`` arr = vec_tor.toArray(arr);

`` System.out.println("The arr[] is:");

`` for ( int j = 0 ; j < arr.length; j++)

`` System.out.println(arr[j]);

`` }

`` catch (Exception e) {

`` System.out.println("Exception: " + e);

`` }

`` }

}

Output:

The Vector: [Welcome, To, Geeks, For, Geeks] Exception: java.lang.NullPointerException