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.
-
Import Statements:
- The program imports
java.util.*
for using classes likeArrayList
,Collections
,Iterator
, andListIterator
.
- The program imports
-
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
andListIterator
. - Use
ArrayList
when the number of elements in a list is not fixed or predetermined. - An
ArrayList
namedaList
is created:ArrayList aList = new ArrayList();
. - Initially, it is empty.
-
Adding Elements:
- Elements are added using the
add()
method:aList.add(11);
: Adds11
at the end of the list.aList.add(0, 21);
: Adds21
at index0
.aList.add(aList.size(), 31);
: Adds31
at the end of the list by specifying the last index.aList.add(41);
: Adds41
at the end.
- Elements are added using the
-
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)
.
- Sorting, reversing, and clearing elements using utility methods from the
-
Traversing the List:
- Using
get()
Method:- The
get()
method is used in afor
loop to iterate over and print all the elements in the list.
- The
- Using
Iterator
Interface:- An
Iterator
is used to traverse the list in a forward direction usingitr.hasNext()
anditr.next()
.
- An
- Using
ListIterator
Interface:- A
ListIterator
traverses the list:- Forward Direction: By calling
litr.hasNext()
andlitr.next()
. - Backward Direction: By calling
litr.hasPrevious()
andlitr.previous()
.
- Forward Direction: By calling
- A
- Using
-
Removing All Elements:
- All elements are removed using
aList.removeAll(aList)
. - The empty list is printed:
System.out.println(aList);
.
- All elements are removed using
**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:
[]