java.util.ArrayList add() Method Example

In this tutorial we will learn add() method of java.util.ArrayList class. This method inserts the element into the ArrayList.

Signature

public boolean add(E e) 

This method appends the specified element to the end of this list.e is the element which will be appended and will return true on successful insertion.

Example

The following example shows the usage of java.util.Arraylist.add(e) method.

import java.util.ArrayList;

public class AddMethodArrayListExample 
{
	public static void main(String args[])
	{
		// create an empty array list
		ArrayList al = new ArrayList();
		
		// use add() method to add elements to the list
		al.add("Element1");
		al.add("Element2");
		al.add("Element3");
		
		//Printing the elements of the list
		System.out.println("Elements of the list");
		for(String temp:al)
		{
			System.out.println(temp);
		}
	}
}

Output

Elements of the list
Element1
Element2
Element3

Leave a Reply

Your email address will not be published. Required fields are marked *