ArrayList Internal working

ArrayList Internal working

Java ArrayList is a part of the Java collections framework and it is a class of java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. The main advantage of ArrayList in Java is, that if we declare an array then we need to mention the size, but in ArrayList, it is not needed to mention the size of ArrayList. If you want to mention the size then you can do it.

ArrayList is a resizable array implementation in Java. ArrayList grows dynamically and ensures that there is always a space to add elements. The backing data structure of ArrayList is an array of Object classes. ArrayList class in Java has 3 constructors. It has its version of readObject and writeObject methods. Object Array in ArrayList is transient. It implements RandomAccess, Cloneable, and java.io.Serializable (which are Marker Interface in Java)


Keyword transient -  At the time of serialization, if we don’t want to save the value of a particular variable in a file, then we use the transient keyword. When JVM comes across a transient keyword, it ignores the original value of the variable and saves the default value of that variable data type.

Marker Interface -  An empty interface (no field or methods). Examples of marker interfaces are Serializable, Cloneable, and Remote interfaces. All these interfaces are empty interfaces.


public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable        

Internally an ArrayList uses an Object[] Array which is an array of objects. All operation like deleting, adding, and updating the elements happens in this Object[] array.


Key Features of ArrayList -

1. Dynamic Growth - The backing data structure of ArrayList is an array of Object class, which dynamically grows as elements are added.


2. Constructors -

a. Default Constructor -  Initializes with a default capacity of 10.

b. Parameterized Constructor - Allows specifying initial capacity.

c. Collection Constructor - Initializes with elements from a specified collection.


3. Add Elements -

a. Uses the “add()” method which checks and ensures capacity.

b. If capacity is insufficient, it grows by 50% using the grow() method.


4. Remove Elements -

a. Uses “remove(int index)” or “remove(Object o)”

b. Shifts subsequent elements left, filling the gap.


5. Performance - 

    a. “add()” - O(1) for single element, O(n) for n elements.

    b. “get()” - O(1)

   c. “remove()” - O(n)

    d. “contains()”, “indexOf()” - O(n)


Best Practices -

Initial Capacity - Specify initial capacity if the size is known in advance to avoid repeated resizing and improve performance.

Performance Considerations - Understand time complexities to optimize usage.

Example - Java

import java.util.ArrayList;
import java.util.Collection;
public class Main {
    public static void main(String args[]) {
        Collection<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println("This is arr: " + arr);

        ArrayList<Integer> newList = new ArrayList<>(arr);
        System.out.println("This is newList: " + newList);
        
        newList.add(7);
        newList.add(700);
        System.out.println("This is newList after adding elements: " + newList);
    }
}
        


ArrayList in Java offers flexibility and efficiency with dynamic resizing and easy manipulation of elements. Understanding its internal workings can significantly enhance your Java development skills.

Connect with me to learn more about Java and backend development!

 #Java #Programming #ArrayList #JavaDevelopment #Coding #TechTips

To view or add a comment, sign in

More articles by Amit Kushwaha

  • Java Garbage Collection

    📒 Java Garbage Collection – Complete Notes (Java 8+ Focus) 1. Heap vs Stack Memory Heap Memory Stores objects and…

    2 Comments
  • 🔐 Spring Security in Microservices — What's the Best Way in 2025?

    If you're still using WebSecurityConfigurerAdapter in your Spring Security configuration, it's time for an upgrade. As…

    1 Comment
  • Kafka Streams

    🔶 What is Kafka Streams? Why do we need it when Kafka already exists? If you're using Apache Kafka, you're already…

  • Top 10 Interview Questions and Answer of Postman in 2024

    1. What is an API? An API, or application programming interface, is a set of protocols that allows different software…

  • Google Associate Cloud Engineer Certification

    The Google Cloud Associate Program is most likely referring to the Google Associate Cloud Engineer certification. This…

  • Circuit Breakers in Spring Boot!

    🚀 Elevate Your Microservices Game with Circuit Breakers in Spring Boot! 🌐 Hey LinkedIn Fam! 👋 As a Senior Software…

Explore content categories