Collections

Arraylist Program in Java Code With Examples

Table of Contents

Program

import java.util.ArrayList;

public class ArrayListExample {
	public static void main(String args[]) {
		ArrayList<Integer> arrlist1 = new ArrayList<Integer>(5);
		

                //add​(E e)
		arrlist1.add(12);
		arrlist1.add(20);
		arrlist1.add(45);
		System.out.println("Printing list1:"+arrlist1);
		ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5);
		arrlist2.add(25);
		arrlist2.add(30);
		arrlist2.add(31);
		System.out.println("\nPrinting list2:"+arrlist2);
		
                //addAll​(int index, Collection<? extends E> c)
                arrlist1.addAll(arrlist2);
		System.out.println("\nPrinting all the elements"+arrlist1);	
		
		//contains​(Object o)
		System.out.print("\nIs 30 present in the arraylist: ");
        System.out.println(arrlist1.contains(30));
        
        // indexOf​(Object o)
        int pos =arrlist1.indexOf(45);
        System.out.println("\nThe element 45 is at index : " + pos);
        
        //isEmpty()
        System.out.println("\nIs ArrayList Empty: "+arrlist1.isEmpty());      
	}
}

Output

Printing list1:[12, 20, 45]

Printing list2:[25, 30, 31]

Printing all the elements[12, 20, 45, 25, 30, 31]

Is 30 present in the arraylist: true

The element 45 is at index : 2

Is ArrayList Empty: false    

Description

public boolean add​(E e)

Appends the specified element to the end of this list.

Specified by:

add in interface Collection<E>

Specified by:

add in interface List<E>

Overrides:

add in class AbstractList<E>

Parameters:

e – element to be appended to this list

Returns:

true (as specified by Collection.add(E))

public boolean addAll​(int index, Collection c)

Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection’s iterator.

Specified by:

addAll in interface List<E>

Overrides:

addAll in class AbstractList

Parameters:

index – index at which to insert the first element from the specified collection
c – collection containing elements to be added to this list

Returns:

true if this list changed as a result of the call

Throws:

IndexOutOfBoundsException – if the index is out of range (index < 0 || index > size())
NullPointerException – if the specified collection is null

public boolean contains​(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).

Specified by:

contains in interface Collection<E>

Specified by:

contains in interface List<E>

Overrides:

contains in class AbstractCollection<E>

Parameters:

o – element whose presence in this list is to be tested

Returns:

true if this list contains the specified element

public int indexOf​(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

Specified by:

indexOf in interface List<E>

Overrides:

indexOf in class AbstractList<E>

Parameters:

o – element to search for

Returns:

the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element

public boolean isEmpty()

Returns true if this list contains no elements.

Specified by:

isEmpty in interface Collection<E>

Specified by:

isEmpty in interface List<E>

Overrides:

isEmpty in class AbstractCollection<E>

Returns:

true if this list contains no elements