
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you add an element to a list in Java?
List in Java is part of the Java Collections Framework, and it is used for storing elements in a sequential manner. It allows duplicate elements but keeps the order of insertion. The List interface is implemented by various classes such as ArrayList, LinkedList, and Vector.
In this article, we will learn how to add an element to a list in Java.
Ways to Add an Element to a List in Java
Following are the different ways to add an element to a list in Java:
Using add() Method
The add() method is used for adding an element to the end of the list. It is a part of the List interface in Java. Following is the syntax of this method -
List.add(E element)
Example
In the below example, we will create a list of integers and then use the add() method to add an element to that list.
import java.util.ArrayList; import java.util.List; public class AddElementToList { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); System.out.println("List after adding elements: " + list); } }
Output
Following is the output of the above code:
List after adding elements: [1, 2, 3, 4]
Using addAll() Method
The addAll() method allows us to add more than one element to the list at once. It takes a collection as an argument and adds all elements of that collection to the list. Following is the syntax of this method -
List.addAll(Collection<? extends E> c)
Example
In the example below, we will create a list of integers and then use the addAll() method to add multiple elements to that list.
import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class AddMultipleElementsToList { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); System.out.println("List before adding elements: " + list); // Adding multiple elements using addAll() list.addAll(Arrays.asList(3, 4, 5)); System.out.println("List after adding elements: " + list); } }
Output
Following is the output of the above code:
List before adding elements: [1, 2] List after adding elements: [1, 2, 3, 4, 5]
Using add(int index, E element) Method
The add() method has another variant that accepts the index value and an element as parameters and inserts the specified element at the specified position. This operation shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Following is the syntax of this method -
List.add(int index, E element)
Example
In the below example, we will create a list of integers and then use the add(int index, E element) method to add an element at a specific index in that list.
import java.util.ArrayList; import java.util.List; public class AddElementAtIndex { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); System.out.println("List before adding element at index 1: " + list); // Adding an element at index 1 list.add(1, 4); System.out.println("List after adding element at index 1: " + list); } }
Output
Following is the output of the above code:
List before adding element at index 1: [1, 2, 3] List after adding element at index 1: [1, 4, 2, 3]