Java Program to demonstrate ArrayList

Program

package ALDemo;
import java.util.*;
public class ArrayListDemo
{
	public static void main(String[] args)
	{
		ArrayList aList = new ArrayList();
		aList.add(11);
		aList.add(0, 21);
		aList.add(aList.size(), 31);
		aList.add(41);
		System.out.println(aList);
		Collections.sort(aList);
		Collections.reverse(aList);
		System.out.println("Traverse using get() method:");
		for(int i = 0; i < aList.size(); i++)
			System.out.println(aList.get(i));
		System.out.println("Traverse by iterator interface:");
		Iterator itr = aList.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
		System.out.println("Traverse by ListIterator interface forward direction:");
		ListIterator litr = aList.listIterator();
		while(litr.hasNext())
			System.out.println(litr.next());
		System.out.println("Traverse by ListInterator interface backward direction:");
		while(litr.hasPrevious())
			System.out.println(litr.previous());
		aList.removeAll(aList);
		System.out.println("After removing all the items from the list:");
		System.out.println(aList);
	}
}

This Java program demonstrates the usage of ArrayList, a part of the java.util package, which is a resizable array implementation in Java.

  1. Import Statements:

    • The program imports java.util.* for using classes like ArrayList, Collections, Iterator, and ListIterator.
  2. Creation of ArrayList:

    • A resizable array that allows dynamic addition and removal of elements.
    • Elements can be accessed using an index or iterated using interfaces like Iterator and ListIterator.
    • Use ArrayList when the number of elements in a list is not fixed or predetermined.
    • An ArrayList named aList is created: ArrayList aList = new ArrayList();.
    • Initially, it is empty.
  3. Adding Elements:

    • Elements are added using the add() method:
      • aList.add(11);: Adds 11 at the end of the list.
      • aList.add(0, 21);: Adds 21 at index 0.
      • aList.add(aList.size(), 31);: Adds 31 at the end of the list by specifying the last index.
      • aList.add(41);: Adds 41 at the end.
  4. Sorting and Reversing:

    • Sorting, reversing, and clearing elements using utility methods from the Collections class.
    • The list is sorted in ascending order using Collections.sort(aList).
    • It is then reversed using Collections.reverse(aList).
  5. Traversing the List:

    • Using get() Method:
      • The get() method is used in a for loop to iterate over and print all the elements in the list.
    • Using Iterator Interface:
      • An Iterator is used to traverse the list in a forward direction using itr.hasNext() and itr.next().
    • Using ListIterator Interface:
      • A ListIterator traverses the list:
        • Forward Direction: By calling litr.hasNext() and litr.next().
        • Backward Direction: By calling litr.hasPrevious() and litr.previous().
  6. Removing All Elements:

    • All elements are removed using aList.removeAll(aList).
    • The empty list is printed: System.out.println(aList);.

**Output**
```shell
$ javac -d . ArrayListDemo.java
$ java ALDemo.ArrayListDemo 
[21, 11, 31, 41]
Traverse using get() method:
41
31
21
11
Traverse by iterator interface:
41
31
21
11
Traverse by ListIterator interface forward direction:
41
31
21
11
Traverse by ListInterator interface backward direction:
11
21
31
41
After removing all the items from the list:
[]