The toArray() method of CopyOnWriteArrayListin Java

Java 8Object Oriented ProgrammingProgramming

The toArray() method is used to return an array containing all the elements in this list in proper sequence.

The syntax is as follows −

Object[] toArray()

To work with CopyOnWriteArrayList class, you need to import the following package −

import java.util.concurrent.CopyOnWriteArrayList;

The following is an example to implement CopyOnWriteArrayList class toArray() method in Java−

Example

 Live Demo

import java.util.Arrays;
import java.util.concurrent.CopyOnWriteArrayList;

public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(220);
      arrList.add(250);
      arrList.add(400);
      arrList.add(500);
      arrList.add(650);
      arrList.add(700);
      arrList.add(800);
      System.out.println("CopyOnWriteArrayList String Representation = " + arrList.toString());
      Object[] myArr = arrList.toArray();
      System.out.println("Array = "+Arrays.toString(myArr));
   }
}

Output

CopyOnWriteArrayList String Representation = [220, 250, 400, 500, 650, 700, 800]
Array = [220, 250, 400, 500, 650, 700, 800]
raja
Published on 27-Mar-2019 10:27:22
Advertisements