Java Vector toArray() Method

The toArray() Java Vector class method is used to get an array containing all of the elements in this vector in the correct order. There is two different types of Java toArray() method which can be differentiated depending on its parameter. These are:

  1. Java Vector toArray() Method
  2. Java Vector toArray(T[] arr) Method

toArray() Method:

This method is used to return an array which contains all of the elements in this vector in the correct order.

toArray(T[] arr) Method:

This method is used to return an array which contains all of the elements in this vector in the correct order. The runtime type of the returned array is that of the specified array.

Syntax

Following is the declaration of toArray() method:

Parameter

ParameterDescriptionRequired/Optional
arrIt is an array into which the elements of a vector are stored if it is big enough. Otherwise, a new array of the same runtime type is allocated for this purpose.Required

Return

The toArray() method returns an array containing all of the elements in the given collection.

Exceptions

NullPointerException- This method has thrown an exception if the specified array is null.

ArrayStoreException- This method has thrown an exception if the runtime type of arr, <T>, is not a supertype of the runtime type, <E>, of every element in this vector.

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Elements are: 
1
2
3
4
5

Example 2

Test it Now

Output:

Contents of a vector are: 
Java
Android
Python
COBOL
Elements of the array: 
Java
Android
Python
COBOL

Example 3

Test it Now

Output:

The elements are:
10
30
20
40
null

Next TopicJava Vector




Latest Courses