
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do I add an element to an array list in Java?
Solution
We can add element to an array list easily using its add() method.
Syntax
boolean add(E e)
Appends the specified element to the end of this list.
Type Parameter
E − The runtime type of the element to be added.
Parameters
e − Element to be appended to this list
Returns
It returns true.
Throws
UnsupportedOperationException − If the add operation is not supported by this list
ClassCastException − If the class of the specified element prevents it from being added to this list
NullPointerException − If the specified element is null and this list does not permit null elements
IllegalArgumentException − If some property of this element prevents it from being added to this list
Example
The following example shows how to add elements to the list using add() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<Integer> list = new ArrayList<>(); // add elements to the list list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); // print the list System.out.println(list); } }
Output
This will produce the following result −
[1, 2, 3, 4, 5, 6]
- Related Articles
- How do you add an element to a list in Java?
- How do I find an element in Java List?
- In PHP, how can I add an object element to an array?
- How do I turn a list into an array in Java?
- Add an element to an array in Swift
- How do I reverse an int array in Java
- How do I declare and initialize an array in Java?
- How do you copy an element from one list to another in Java?
- How do I remove a particular element from an array in JavaScript
- How do I add a value to the top of an array in MongoDB?
- How do I add a simple onClick event handler to an HTML5 canvas element?
- How to remove an element from an array in Java
- How to add description list of an element using HTML?
- How to remove an element from Array List in C#?
- Add an element to a Stack in Java

Advertisements