• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Use the java.util.ArrayList Class

August 31, 2023 //  by Talha Malik

In Java, “ArrayList” corresponds to the container containing the values of multiple data types. It is such that this functionality is effective in invoking the elements in the order of insertion and supports the null values as well. Also, these are helpful in scenarios where lots of manipulation and updating in the array is required.

This tutorial explains the working of the “java.util.ArrayList” class.

How to Use the “java.util.ArrayList” Class?

“ArrayList” in Java is included in the Java collection framework and is contained in the “java.util” package. The ArrayList class is implemented via the “List” interface. It gives the feature of a dynamic array where the size is not fixed like an array. 

Syntax

ArrayList<T> arrayList= new ArrayList<>();

In the given syntax, “T” refers to the ArrayList’s data type, “new” is the keyword, and “ArrayList()” corresponds to the constructor.

ArrayList Class Constructors

The below table showcases different ArrayList constructors, along with their respective functionalities:

ConstructorFunctionality
ArrayList()It creates a null array list.
ArrayList(Collection<? extends E> c)It creates an ArrayList initialized with the items of the collection c.
ArrayList(int cap)It creates an ArrayList having the specified initial capacity.

ArrayList Class Methods

The table below presents various ArrayList methods along with their respective functionalities in Java:

MethodFunctionality
size()It retrieves the ArrayList’s length.
clone()This method creates a new ArrayList with the same item, size, and capacity.
add(Object x)This method adds the target item at the end of the list.
sort()It sorts the ArrayList items.
set(int ind, I item)It replaces the item at the target position in the list with the particular item.
contains(Object x)It searches the ArrayList for the target element and gives the corresponding Boolean outcome.
ensureCapacity(int x)It specifies the total items the ArrayList can comprise.
isEmpty()This method analyzes if the ArrayList is null/empty.
indexOf(Object x)It searches the target item in the ArrayList and retrieves its index.
clear()This method removes all the list items.
remove(int x)It removes the first occurrence of the target element.
removeAll(Collection x)It removes all the list items.

In the next section, the discussed “ArrayList” class methods will be utilized to perform various operations on the list.

Example 1: Adding and Removing Elements From the ArrayList

This example adds and removes the elements from an ArrayList:

import java.util.*;
public class Arraylist {
public static void main(String args[]){
  ArrayList<Integer> lt = new ArrayList<>();
  lt.add(1);
  lt.add(2);
  lt.add(3);
  lt.add(4);
  lt.add(5);
  System.out.println("ArrayList after Insertion of Elements -> "+lt);
  lt.remove(4);
  System.out.println("ArrayList after Removal of Element -> "+lt);
}}

According to the above code lines:

  • Create an ArrayList of “Integer” type.
  • After that, insert the stated integers in the list using the “add()” method and display them.
  • Now, apply the “remove()” method to remove the list element at the target index i.e., “4”.
  • Lastly, display the resultant ArrayList after the removal of the item.

Output

This output signifies that the elements are added and the target element is removed from the list appropriately.

Example 2: Iterating the ArrayList Elements and Sorting Them

In this approach, the ArrayList elements will be iterated via the “for” loop and, analyzed for size and sorted:

import java.util.*;
public class Arraylist {
public static void main(String args[]){
  ArrayList<Integer> lt = new ArrayList<>();
  lt.add(1);
  lt.add(3);
  lt.add(5);
  lt.add(4);
  lt.add(2);
  System.out.println("Iterated Elements Before Sorting -> "+lt);
  System.out.println("List Size -> "+lt.size());
  for (int i = 0; i < lt.size(); i++) {
  Collections.sort(lt);
  System.out.print(lt.get(i) + " ");
}
}}

In this block of code:

  • Create an ArrayList and append the provided integers in it.
  • Also, return its(ArrayList) size via the “size()” method.
  • Apply the combined “for” loop and the “Collections.sort()” method to iterate the ArrayList and sort it, respectively.
  • Finally, return the resultant sorted ArrayList.

Output

Example 3: Updating the ArrayList Elements

In this specific demonstration, the added ArrayList elements will be updated or changed via the “set()” method:

import java.util.*;
public class Arraylist {
public static void main(String args[]){
  ArrayList<Integer> lt = new ArrayList<>();
  lt.add(1);
  lt.add(2);
  lt.add(3);
  lt.add(4);
  lt.add(5);
  System.out.println(lt.isEmpty());
  System.out.println("Added ArrayList Elements -> "+lt);
  lt.set(2, 25);
  System.out.println("Updated ArrayList Elements -> "+lt);
}}

In this block of code:

  • Repeat the discussed approaches for creating an ArrayList and inserting elements in it.
  • Now, analyze if the list is empty using the “isEmpty()” method.
  • After that, apply the “set()” method to update the added element by referring to its index i.e., “2”.
  • This resultantly updates the value at the target index in the list.

Output

From the outcome, it can be observed that the value at the index “2” i.e., 3 is updated accordingly.

Conclusion

ArrayList in Java is contained in the “java.util” package and comprises methods such as “add()”, “remove()” etc. to perform operations on the created list. This blog has discussed the working of the “java.util.ArrayList” class.

Category: Java

Previous Post: « How to Use the Java String getBytes() Method
Next Post: How to Use ZonedDateTime now() Method in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact