java arraylist add list code example

Example 1: how to add to an arraylist java

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

        //ArrayList<String> Declaration
        ArrayList<String> al= new ArrayList<String>();
        //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);

Example 2: arraylist add method

public void add(int index, E element)	// no return value

Example 3: how to add all list elements at once in java

import java.util.*;
List<Integer> l = new ArrayList<Integer>();
l.addAll(new ArrayList<Integer>(Arrays.asList(5,6,8,11)));

Tags:

Java Example