- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The add() method of CopyOnWriteArrayList in Java
Two types of add() methods are available in the CopyOnWriteArrayList class: add(E e) method
To add elements in CopyOnWriteArrayList class in Java, use the add() method. Here, the element is appended to the end of the list.
The syntax is as follows
boolean add(E e)
Here, the parameter e is the element to be appended to this list.
To work with CopyOnWriteArrayList class, you need to import the following package
import java.util.concurrent.CopyOnWriteArrayList;
The following is an example to add elements in the CopyOnWriteArrayList class in Java
Example
import java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>(); arrList.add(100); arrList.add(250); arrList.add(400); arrList.add(500); arrList.add(650); arrList.add(700); arrList.add(800); System.out.println("CopyOnWriteArrayList Elements = " + arrList); } }
Output
CopyOnWriteArrayList Elements = [100, 250, 400, 500, 650, 700, 800]
add(int index, E element) method
Using this method, insert the specified element at the specified position in the CopyOnWriteArrayList.
The syntax is as follows
void add(int index, E ele)
Here, the parameter index is where you want to insert the element and ele is the element to be inserted to this list.
To work with CopyOnWriteArrayList class, you need to import the following package
import java.util.concurrent.CopyOnWriteArrayList;
The following is an example to add elements in the CopyOnWriteArrayList class in Java
Example
import java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>(); arrList.add(100); arrList.add(250); arrList.add(0, 400); arrList.add(1, 500); arrList.add(2, 650); arrList.add(700); arrList.add(800); System.out.println("CopyOnWriteArrayList Elements = " + arrList); } }
Output
CopyOnWriteArrayList Elements = [400, 500, 650, 100, 250, 700, 800]
- Related Articles
- The isEmpty() method of CopyOnWriteArrayList method in Java
- The hashCode() method of CopyOnWriteArrayList method in Java
- The clone() method of CopyOnWriteArrayList method in Java
- The contains() method of CopyOnWriteArrayList in Java
- The toString() method of CopyOnWriteArrayList in Java
- The set() method of CopyOnWriteArrayList in Java
- The addIfAbsent() method of CopyOnWriteArrayList in Java
- The iterator() method of CopyOnWriteArrayList in Java
- The lastIndexOf() method of CopyOnWriteArrayList in Java
- The listIterator() method of CopyOnWriteArrayList in Java
- The get() method of CopyOnWriteArrayList in Java
- The indexOf() method of CopyOnWriteArrayList class in Java
- The toArray(T[] a) method of CopyOnWriteArrayList in Java
- How to add elements in Java CopyOnWriteArrayList?
- The listIterator() method of CopyOnWriteArrayList in Java starting at a specified position
