Java CopyOnWriteArrayList toArray() Method

The toArray() method of Java CopyOnWriteArrayList class returns an array which contains all of the elements in this list in proper sequence from first to the last element. The toArray() method acts as a bridge between array-based and collection-based APIs.

Syntax:

Returns:

This method returns an array which contains all the elements in this list.

Example 1

Test it Now

Output:

Array List is : [1, 2, 1, 4, 5, 3, 3, 8]

Example 2

Test it Now

Output:

Array List is : []

Example 3

Test it Now

Output:

Array List is : [Tom, Harry, John, John, John, Jerry]

Example 4

Test it Now

Output:

Array List is : []

Java CopyOnWriteArrayList<E>toArray(T[] a) Method:

The toArray(T[] a) method of Java CopyOnWriteArrayList class returns an array which contains all of the elements in this list in proper sequence from first to the last element. The runtime type of the returned array is that of the specified array.

This method returns therein if the list fits in the specified array. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

Syntax:

Parameters:

a - It is the array into which the elements of the list are to be stored.

Returns:

This method returns an array which contains all the elements in this list.

Example 1

Test it Now

Output:

Array List is : [1, 2, 1, 4, 5, 3, 3, 8, null, 6, 6, 6, 6, 6, 6]

Example 2

Test it Now

Output:

Array List is : [1, 2, 1, 4, 5, 3, 3, 8]

Example 3

Test it Now

Output:

Array List is : [Tom, Harry, John, John, John, Jerry, null, 6, 6, 6, 6, 6, 6]

Example 4

Test it Now

Output:

Array List is : [Tom, Harry, John, John, John, Jerry]





Latest Courses