Table of contents
1.
Introduction
2.
ArrayList
3.
Constructors
4.
Methods
5.
Basic operations
5.1.
Add/Update elements 
5.2.
Access elements 
5.3.
Remove elements 
6.
FAQs
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

ArrayList in Java

Author Yashesvinee V
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The List interface in Java defines an ordered collection of elements. The ArrayList is a resizable-array implementation of the List interface that is part of the java.util package. It implements all list operations and permits the use of null and duplicate elements. The ArrayList class provides functions/methods to manipulate the size and contents of the list.

ArrayList

  • ArrayList helps us create dynamic arrays and hence, does not require us to specify the size during creation. Although every ArrayList has a capacity, the size increases and decreases as we add or remove elements.
     
  • ArrayLists uses wrapper classes instead of primitive data types like int, float, char or double. 
     
  • The ArrayList implementation is not synchronised. When multiple threads access an ArrayList concurrently, and at least one of the threads structurally modifies the list, it must be synchronised externally. The equivalent synchronised class of ArrayList is the Vector class.
     

Declaration:

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable 
You can also try this code with Online Java Compiler
Run Code

Constructors

 

Must Read Type Conversion in Java and  Swap Function in Java

Methods

 

Must Read Difference between ArrayList and LinkedListDuck Number in Java

Basic operations

Add/Update elements
 

import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    // create ArrayList
    ArrayList<String> languages = new ArrayList<>();

    // add() method without the index parameter
    languages.add("Arabic");
    languages.add("Chinese");
    languages.add("Hindi");

    System.out.println("List: " + languages);

    languages.set(1,"English");

    languages.replaceAll(e -> e.toUpperCase());

    System.out.println("List: " + languages);
  }
}
You can also try this code with Online Java Compiler
Run Code


Output:

List: [Arabic, Chinese, Hindi]
List: [ARABIC, ENGLISH, HINDI]

 

Access elements
 

import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    ArrayList<String> languages = new ArrayList<>();

    languages.add("Arabic");
    languages.add("Malayalam");
    languages.add("Hindi");

    System.out.println("1: " + languages.get(0));
    System.out.println("2: " + languages.get(1));
    System.out.println("3: " + languages.get(2));

    languages.forEach((e) -> {
      e = e.toUpperCase();
      System.out.print(e + "\n");
    });

    System.out.println("Indian Languages: " + languages.subList(1,3));
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

1: Arabic
2: Malayalam
3: Hindi
ARABIC
MALAYALAM
HINDI
Indian Languages: [Malayalam, Hindi]


You can compile java code just by clicking here.

Remove elements
 

import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    ArrayList<String> languages = new ArrayList<>();

    languages.add("Arabic");
    languages.add("Malayalam");
    languages.add("Hindi");
    System.out.println("Languages: " + languages);
    
    System.out.println("Removing " + languages.remove(0));
    
    System.out.println("Removing Malayalam...");
    languages.removeIf(e -> e.startsWith("Mal"));
    
    System.out.println("Languages: " + languages);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Languages: [Arabic, Malayalam, Hindi]
Removing Arabic
Removing Malayalam...
Languages: [Hindi]


Try this code by yourself on Online Java Compiler.

Must Read:  Java List Iterator and Hashcode Method in Java

FAQs

  1. What is the difference between an array and ArrayList?

    An array is a basic data structure provided by Java that has a fixed length. In contrast, ArrayList is a class of Java Collections framework that has a variable length. An Array can store values of primitive data types while ArrayList uses Wrapper classes.
     
  2. What is the difference between iterator and listIterator?

    Iterator can traverse the array elements in the forward direction and can perform only remove operations. ListIterator can traverse the array of elements in backward as well as forward directions. It can perform add, remove, and set operations while traversing the collection.

Conclusion

This article extensively discusses ArrayList in Java. We hope that this blog has helped you enhance your knowledge about the different methods and ways of implementing ArrayLists in Java. If you would like to learn more, check out our articles Arrays and Wrapper classes

Recommended problems -

 

Explore our Coding Ninjas Library for more articles like these and do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass