Useful ArrayList Java(SE 8) Programming Examples

In this article, i will take you through useful ArrayList Java Programming Examples. Arraylist allows us to store data dynamically and is resizable in nature. It uses dynamic arrays for storing the elements. It is a part of Collection framework present in java.util package. It inherits AbstractList class and implements List Interface. It might be slower that arrays but is very helpful in programs where lot of array manipulation is required. It also allows us to randomly access the list.

Useful ArrayList Java(SE 8) Programming Examples 1

ArrayList Java Programming Examples

Also Read: Useful LinkedList Java Programming Examples

boolean add(E e)

If you want to add element in an ArrayList, then you need to use ArrayList Java Class add() method as shown in below program.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
System.out.println("List of Elements added is:" + li);
}
}

Output

List of Elements added is:[3, 4]

void add(int index, E element)

If you want to add an element at a particular index in an ArrayList, you need to use ArrayList Java Class add(int index,E element) method as shown in below program. Here we are adding element 9 at index 1 by passing these as arguments in add method.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
System.out.println("List of Elements added is:" + li);
li.add(1,9);
System.out.println("List of Elements after adding element at index 1: " + li);
}
}

Output

List of Elements added is:[3, 4]
List of Elements after adding element at index 1: [3, 9, 4]

boolean contains(Object o)

If you want to check an element is present in the ArrayList or not, you need to use ArrayList Java Class contains(Object o) method as shown in below program. In the below program, we are checking if the element 4 is available in the ArrayList or not.

Advertisements
import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
System.out.println("List of Elements added is:" + li);
System.out.println("Is List contain element 4: " + li.contains(4));
}
}

Output

List of Elements added is:[3, 4]
Is List contain element 4: true

int indexOf(Object o)

If you want to find the Index of an element in an ArrayList, then you need to use ArrayList Java Class indexOf(Object o) method as shown in the below program. Here we are checking the index number of element 4. As you can see from the output, it turns out that index number of element 4 is 1.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
li.add(9);
System.out.println("List of Elements added is:" + li);
System.out.println("Index of Element 4 is: " + li.indexOf(4));
}
}

Output

List of Elements added is:[3, 4, 9]
Index of Element 4 is: 1

boolean isEmpty()

If you want to check if an ArrayList is empty or not, you can use ArrayList Java Class isEmpty() method as shown in below program. This method check if the ArrayList has any element in it or it is completely empty. It will return true if it is completely empty or false if it contains atleast one element.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
li.add(9);
System.out.println("List of Elements added is:" + li);
System.out.println("Is List is Empty ? " + li.isEmpty());
}
}

Output

List of Elements added is:[3, 4, 9]
Is List is Empty ? false

int lastIndexOf(Object o)

If you want to find the last occurrence of an element in an ArrayList, then you need to use ArrayList Java Class lastIndexOf(Object o) method as shown in below program. In this program, we are checking the index of last occurrence of Element 4. Since there is only one 4 at index 1 in the ArrayList, so it will show index 1 at the output.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
li.add(9);
System.out.println("List of Elements added is:" + li);
System.out.println("Index of Element 4 is: " + li.lastIndexOf(4));
}
}

Output

List of Elements added is:[3, 4, 9]
Index of Element 4 is: 1

E remove(int index)

If you want to remove an element based on Index number from an ArrayList, then you need to use ArrayList Java Class remove(int index) method as shown in below program.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
li.add(9);
System.out.println("List of Elements added is:" + li);
System.out.println("Remove Index 2 Element: " + li.remove(2));
System.out.println("List After Removing Element: " + li);
}
}

Output

List of Elements added is:[3, 4, 9]
Remove Index 2 Element: 9
List After Removing Element: [3, 4]

int size()

If you want to find the size of an ArrayList, then you need to use ArrayList Java Class size() method as shown in below program. We have added 3 elements in the ArrayList by using add() method thrice, hence you will see the size equal to 3 as evident from the output.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
li.add(9);
System.out.println("List of Elements added is:" + li);
System.out.println("Size of the List is: " + li.size());
}
}

Output

List of Elements added is:[3, 4, 9]
Size of the List is: 3

E set(int index, E element)

If you want to set an element at a particular Index in an ArrayList, then you may want to use ArrayList Java Class set(int index, E element) method as shown in below program. Here you can see that set() method is replacing 4 by 45 as we have passed index 1 as an argument to set the element 45 at index 1.

import java.util.ArrayList;

class ArrayLis
{
public static void main(String[] args)
{
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(3);
li.add(4);
li.add(9);
System.out.println("List of Elements added is:" + li);
System.out.println("Setting Element at Index 1: " + li.set(1,45));
System.out.println("List of Element: " + li);
}
}

Output

List of Elements added is:[3, 4, 9]
Setting Element at Index 1: 4
List of Element: [3, 45, 9]

 

Reference: More on ArrayList

Advertisements

Leave a Reply