Organizational Research By

Surprising Reserch Topic

Java ArrayList add() Method Example


Here we are discussing about add() method of Java.util.ArrayList class. This method is used for adding an element to the ArrayList. Below is the method signature:

public boolean add(Object element)

Example

package beginnersbook.com;
import java.util.ArrayList;
public class Details {
    public static void main(String[] args) {

        //ArrayList Declaration
        ArrayList al= new ArrayList();
        //add method for String ArrayList
        al.add("Ram");
        al.add("Shyam");
        al.add("CPS");
        al.add("John");
        al.add("Steve");
        System.out.println("Elements of ArrayList of String Type: "+al);

        //ArrayList Declaration
        ArrayList al2 = new ArrayList();
        //add method for integer ArrayList
        al2.add(1);
        al2.add(34);
        al2.add(99);
        al2.add(99);
        al2.add(78);
        System.out.println("Elements of ArrayList of Integer Type: "+al2);
    }
}
Output:

Elements of ArrayList of String Type: [Ram, Shyam, CPS, John, Steve]
Elements of ArrayList of Integer Type: [1, 34, 99, 99, 78]
asked Jun 11, 2015 in Java Interview Questions by rajesh
0 votes
60 views



Related Hot Questions



Government Jobs Opening


...