CopyOnWriteArrayList toArray() method in Java

The toArray() method of CopyOnWriteArrayList is used to return an array containing all the elements in CopyOnWriteArrayList in the correct order.

Syntax:

public Object[] toArray()
           or
public <T> T[] toArray(T[] a)

Parameters: This method either accepts no parameters or it takes an array T[] a as parameter which is the array into which the elements of the list 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 function returns an array containing all the elements in this list.

Exception: The first overload of this method throws no exceptions. However, the second overload throws following exceptions:



  • ArrayStoreException: if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
  • NullPointerException if the specified array is null

Below programs illustrate the CopyOnWriteArrayList.toArray() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the
// CopyOnWriteArrayList toArray() method in Java
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis.add(32);
        ArrLis.add(67);
        ArrLis.add(98);
        ArrLis.add(100);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: "
                           + ArrLis);
  
        // Get the array of the elements
        // of the CopyOnWriteArrayList
        // using toArray() method
        Object[] arr = ArrLis.toArray();
  
        System.out.println("Elements of CopyOnWriteArrayList"
                           + " as Array: "
                           + Arrays.toString(arr));
    }
}

chevron_right


Output:

CopyOnWriteArrayList: [32, 67, 98, 100]
Elements of CopyOnWriteArrayList as Array: [32, 67, 98, 100]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the
// CopyOnWriteArrayList toArray(T[]) method in Java
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis.add(32);
        ArrLis.add(67);
        ArrLis.add(98);
        ArrLis.add(100);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: "
                           + ArrLis);
  
        // Get the array of the elements
        // of the CopyOnWriteArrayList
        // using toArray(T[]) method
        Integer arr[] = new Integer[ArrLis.size()];
        arr = ArrLis.toArray(arr);
  
        System.out.println("Elements of CopyOnWriteArrayList"
                           + " as Array: "
                           + Arrays.toString(arr));
    }
}

chevron_right


Output:

CopyOnWriteArrayList: [32, 67, 98, 100]
Elements of CopyOnWriteArrayList as Array: [32, 67, 98, 100]

Reference: CopyOnWriteArrayList.toArray() doc, CopyOnWriteArrayList.toArray(T[]) doc



My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.