Best ArrayList Class examples in Java tutorial

This tutorial explains ArrayList basics in Java and features and the top best examples.

ArrayList is the implementation of the List interface in the java.util package, storing a collection of objects in the order of insertion. It dynamically grows the size of the list, making it a resizable array, unlike a static array where the size is fixed once values are assigned.

ArrayList is the implementation of the List interface in the java.util package.

It stores the collection of objects in the order of insertion. ArrayList stores the object and grows the size of the list dynamically. Due to this, the array list is a resizable array. whereas an array is static once assigned the values, the size is fixed.

In real-time programming, ArrayList is used to manipulate collections of custom objects, making it a popular framework among Java developers.

Important points to remember for ArrayList

  • Objects stored in ArrayList follow insertion order.
  • ArrayList dynamically increases its size when elements are added and shrinks when elements are deleted.
  • It is not synchronized and does not guarantee sorting.
  • ArrayList stores the objects that include null values.
  • Elements are not sorted by default

Real-time ArrayList Usage examples

In a School Management application, a list of students is stored in the database. To display these students on the user interface, a call to the database retrieves the elements, and an ArrayList is used as a temporary object to hold these elements.

When working on CRUD operations for database entries, ArrayList is employed to store objects in a temporary cache mechanism.

How to create an ArrayList

ArrayList has both a default and parameterized constructor with an integer parameter. It can be initialized with specific sizes as shown below:

ArrayList emptylist=new ArrayList(); or
 ArrayList list=new ArrayList(10);

The above lines of code create an ArrayList object with an empty size as well as the fixed size of the array list. In the second case, the fixed size is 10.

Another way to create and initialize an ArrayList with default values:

ArrayList<String> list = new ArrayList<String>() {{
    add("one");
    add("two");
    add("three");
}};

How to add Objects to ArrayList?

The add method is used to add objects to ArrayList:

ArrayList<String> list=new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");

How to iterate the elements in the ArrayList?

Using a foreach loop

for (Object str : list) {
 System.out.print(" " + (String) str);
}

and the output is

One Two Three Four Five

Always check for null before iterating to avoid NullPointerException:

Add the following code snippet for avoiding NullPointerException in ArrayList


if(list!=null){
  //add the for loop code here
}

How to find the size of ArrayList?

Java ArrayList API provides the size() method to find the number of elements.

list.size()

Return the size of the list, 4 is returned for the above code.

How to check if the object/element is found in ArrayList

It provides the contains() method which returns true if a particular object is found in the system

System.out.print(" " + list.contains("One"));

And also another way to use the indexOf() method to get the index of an element in ArrayList. This method returns position if the element is found in the array list else returns -1 if the element/object is not available in the array list.

How to Convert ArrayList to the array?

An array list to the array is one of the common mechanisms in day-to-day programming.

Collection API provides the ArrayList.toArray() method used to convert to an array of objects


String[] strArray=(String [])list.toArray(new String[list.size()]);
  for (Object str : strArray) {
   System.out.print(" " + (String) str);
  }
`

and the output is


One Two Three Four Five

How to convert the array to ArrayList in Java

to convert the array to ArrayList, the collection API provides java.util.Arraysclass asList() method which accepts ArrayList

String[] strObjects = {"One1", "two2", "three3", "four4"};
List arrayList= new ArrayList(Arrays.asList(strObjects));

How to remove/delete an element in the list

ArrayList has to use the remove method to remove the object from the list.

arrayList.remove("one");

How to get the elements from the list?

The list provides the get() method to return the object found in the list.


list.get("one")

If the element is not found, null is returned.

How to sort objects in ArrayList?

If we are displaying the objects from the list, their order is of insertion order only. To sort the objects in the ArrayList, collection API provides Collections.sort(List) method to sort in the natural order.

Collections.sort(list);   // natural order
Collections.reverse(list);   // natural order

These elements sort in natural order by the hashcode as well equals the method default implementation order.

If we want to display the custom order for primitive types or objects based on member variables, we have to write our custom comparable java class which is passed to the Collections.sort() method as another parameter.

Collections.sort(list,Comparable interface);

java8 Iteration list

Java 8 provides a forEach loop with a lambda expression

list.forEach(item -> System.out.println(item));

I hope this provides a clearer understanding of ArrayList in Java with examples.