Java - Util.Arraylist - Add Method: Description
Java - Util.Arraylist - Add Method: Description
ADD() METHOD
http://www.tuto rialspo int.co m/java/util/arraylist_add.htm
Co pyrig ht tuto rials po int.co m
Description
T he java.util.ArrayList.add(E e) method appends the specified element E to the end of the list.
Declaration
Following is the declaration for java.util.ArrayList.add() method
public boolean add(E e)
Parameters
e -- T he element to be appended to this list.
Return Value
T his method returns true.
Exception
NA
Example
T he following example shows the usag e of java.util.Arraylist.add(E) method.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list with an initial capacity ArrayList<Integer> arrlist = new ArrayList<Integer>(5); // use add() method to add elements in the list arrlist.add(15); arrlist.add(20); arrlist.add(25); // let us print all the elements available in list for (Integer number : arrlist) { System.out.println("Number = " + number); } } }
Let us compile and run the above prog ram, this will produce the following result:
Number = 15 Number = 20 Number = 25