Easy Tutorial
For Competitive Exams

Java Programming ArrayList

What is ArrayList?

ArrayList is a resizable-array implementation of the List interface.

It implements list operations, and permits all elements, including null.

In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

How to perform Basic Operations in ArrayList ?

  • creating object for ArrayList
  • adding objects into ArrayList
  • accessing objects based on index
  • searching an object in ArrayList whether it is listed under this instance or not
  • adding elements at specific index
  • checking whether the ArrayList is empty or not
  • getting object index
  • Get the size of the ArrayList.

Basic Operations

public class MyBasicArrayList {

	public static void main(String[] a){
		
		ArrayList al = new ArrayList();
		//add elements to the ArrayList
		al.add("JAVA");
		al.add("C++");
		al.add("PERL");
		al.add("PHP");
		System.out.println(al);
		//get elements by index
		System.out.println("Element at index 1: "+al.get(1));
		System.out.println("Does list contains JAVA? "+al.contains("JAVA"));
		//add elements at a specific index
		al.add(2,"PLAY");
		System.out.println(al);
		System.out.println("Is arraylist empty? "+al.isEmpty());
		System.out.println("Index of PERL is "+al.indexOf("PERL"));
		System.out.println("Size of the arraylist is: "+al.size());
	}
}
Try it Yourself

How to read all elements in ArrayList by using iterator?

By calling iterator() method we will get Iterator object, through which we can iterate all the elements of the ArrayList.

Also we can iterate the ArrayList based on index too.

Example

import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListIterator {
 
    public static void main(String a[]){
        ArrayList arrl = new ArrayList();
        //adding elements to the end
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        Iterator itr = arrl.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    }
}
Try it Yourself

How to delete all elements from my ArrayList?

For deleting all objects from ArrayList at once by calling clear() method on ArrayList.

Example

import java.util.ArrayList;
 
public class ClearMyArrayList {
 
    public static void main(String a[]){
        ArrayList arrl = new ArrayList();
        //adding elements to the end
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("Actual ArrayList:"+arrl);
        arrl.clear();
        System.out.println("After clear ArrayList:"+arrl);
    }
}
Try it Yourself

How to copy ArrayList to array?

For copying all content of ArrayList to an array is done by calling toArray() method.

Example

import java.util.ArrayList;
 
public class MyArrayListArray {
 
    public static void main(String a[]){
        ArrayList arrl = new ArrayList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("Actual ArrayList:"+arrl);
        String[] strArr = new String[arrl.size()];
        arrl.toArray(strArr);
        System.out.println("Created Array content:");
        for(String str:strArr){
            System.out.println(str);
        }
    }
}
Try it Yourself

How to swap two elements in a ArrayList?

By calling Collections.swap() method we can swap two elements of the ArrayList.

We have to pass the indexes which we need to swap.

Example

import java.util.ArrayList;
import java.util.Collections;
 
public class MyArrayListSwap {
 
    public static void main(String a[]){
        ArrayList list = new ArrayList();
        list.add("Java");
        list.add("Cric");
        list.add("Play");
        list.add("Watch");
        list.add("Glass");
        list.add("Movie");
        list.add("Girl");
         
        Collections.swap(list, 2, 5);
        System.out.println("Results after swap operation:");
        for(String str: list){
            System.out.println(str);
        }
    }
}
Try it Yourself

How to reverse ArrayList content?

We can reverse the content by calling Collections.reverse() method.

We have to pass ArrayList instance to this method, which reverses the content.

Example

import java.util.ArrayList;
import java.util.Collections;

public class MyArrayListReverse {

	public static void main(String a[]){
		ArrayList list = new ArrayList();
		list.add("Java");
		list.add("Cric");
		list.add("Play");
		list.add("Watch");
		list.add("Glass");
		Collections.reverse(list);
		System.out.println("Results after reverse operation:");
		for(String str: list){
			System.out.println(str);
		}
	}
}
Try it Yourself
#20076,20077,20078,20079
Share with Friends