VOOZH about

URL: https://www.geeksforgeeks.org/java/copyonwritearraylist-toarray-method-in-java/

⇱ CopyOnWriteArrayList toArray() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CopyOnWriteArrayList toArray() method in Java

Last Updated : 11 Jul, 2025
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:
Output:
CopyOnWriteArrayList: [32, 67, 98, 100]
Elements of CopyOnWriteArrayList as Array: [32, 67, 98, 100]
Program 2:
Output:
CopyOnWriteArrayList: [32, 67, 98, 100]
Elements of CopyOnWriteArrayList as Array: [32, 67, 98, 100]
Reference: CopyOnWriteArrayList.toArray() doc, CopyOnWriteArrayList.toArray(T[]) doc
Comment