ArrayList Implementation in Java30 Mar 2025 | 7 min read ArrayList is a class of Java Collection framework. It uses a dynamic array for storing the objects. It is much similar to Array, but there is no size limit in it. We can add or remove the elements whenever we want. We can store the duplicate element using the ArrayList; It manages the order of insertion internally. The ArrayList class is much more flexible than the traditional Array. It implements the List interface to use all the methods of List Interface. It takes place in java.util package. Understanding its implementation is fundamental for any Java developer striving for proficiency and elegance in their code. In this section, we will discuss the ArrayList implementation in Java, along with its features, advantages, and best practices. The ArrayList class inherits the AbstractList class and implements the List Interface. The elements of it can be randomly accessed. It cannot be used for primitive types such as int, char, etc.; for these data types, we need a wrapper class. The difference between Array and ArrayList is that Arraylist provides a dynamic array that can be expanded when needed. In Array, we have to specify the size of the Array during initialization, but it is not necessary for ArrayList. By default, it takes its size to 10. ArrayList class can be declared as follows: ArrayList can be defined as follows: Consider the following example: File Name: ArrayListLExample.java Output: List objects are: [Sam, Sandy, Joe, Arya, Nik] After Removing Nik, List Objects are: [Sam, Sandy, Joe, Arya] From the above example, we can see how we can add or remove the List's objects. Let's understand how it works internally: ![]() How ArrayList Works Internally?When we initialize an ArrayList using the below syntax: It creates an Array with the default capacity, which is 10. It invokes the default constructor of the ArrayList class. It uses an empty array instance to create the new object, and the following code is executed by the Java compiler: in Java 7 or previous version: in Java 8 or later version: In the above code snipet, we can see an empty ArrayList is internally created with the default capacity, which is 10. When we add the first element in it, it will be expanded to DEFAULT_CAPACITY. ArrayList uses an Object [] Array to add, remove, and traverse the element. For the prior versions of Java than Java 8, it specifies the objects as follows: In the above code snippet, we can see the array size will be equal to the specified Array. We can also define the List with the specific capacity. When we provide an initial capacity, the ArrayList constructor is invoked internally to specify the Array internally. For example, if we define an array list with the capacity of 20, we have to define the ArrayList as follows: Then the following code will be executed by the Java compiler: In Java 7 or previous versions: From the above code, we can see the array size will be equal to the specified Array. In Java 8 or later versions: From the above code, we can see the array size will be equal to the specified Array. We can also create an object of ArrayList for a specific collection by executing the below line of code: The above code will create a non-empty list having the objects of LinkedList. Key Features of ArrayList
How the ArrayList Grows Dynamically?When we add a new object to the ArrayList, it will check for the initial size of the ArrayList whether it has space or not. If there is enough space for the new object, it will add simply using the add() method. If there is not enough space to add a new object, it will dynamically increase the size of the Array by that much capacity. Consider the following implementation of the add() method (In Java 7 or later): In the above code the above code,
ArrayList Implementation DetailsArrayList in Java is internally backed by an array of objects (Object[]) to store elements. Let's explore some crucial implementation details: Capacity and Size: ArrayList maintains two main parameters: capacity and size. Capacity represents the current size of the internal array, while size denotes the number of elements present in the ArrayList. Resizing Strategy: When elements are added beyond the current capacity, ArrayList automatically increases its capacity to accommodate new elements. It typically doubles the size of the internal array to maintain amortized constant-time complexity for append operations. Performance Considerations: While ArrayList provides efficient random access and insertion/removal at the end of the list, inserting or removing elements from the middle incurs higher computational overhead due to array copying. Iterating through ArrayList: ArrayList offers various ways to iterate through its elements, including traditional for loops, enhanced for loops, and iterators obtained via the iterator() method. Performance of ArrayListIn the ArrayList, the add operation requires o(n) time; other operations are run in linear time. The size, isEmpty, get, set, iterator, and listIterator tasks run in a constant time of O(1). Summary
Its dynamic resizing, efficient random access, and support for generics make it a versatile choice for managing collections of elements. By understanding its internals, leveraging best practices, and being cognizant of performance considerations, developers can harness the full potential of ArrayLists to write efficient and robust Java code. |
Print the entire string s in backward, excluding the first and last words. Examples: Input: Hello, welcome to JavaTpoint Output: Hello, emoclew ot JavaTpoint Input: I am good Output: I ma good Input: I am good at Java Output: I ma doog ta Java The first word is printed as usually. Print the opposite of...
2 min read
Image class in Java is an abstract superclass for all the other classes used for graphical image representation. Class Declaration The declaration for java.awt.Image class is as following: Public abstract class Image extends Object Class Fields Following table shows various fields of Image class. Field Description protected float accelerationPriority It prioritise for accelerating the...
4 min read
Merge sort is a popular sorting algorithm that efficiently sorts an array or a list by dividing it into smaller sub-arrays sorting them independently and then merging them back together. It is renowned for its effectiveness, stability, and capacity for handling huge datasets. By using multithreading...
6 min read
? In this section, we will learn different approaches to convert bytes to hexadecimal in Java. Convert Bytes to Hex There are the following ways to convert bytes to hexadecimal: Using Integer.toHexString() Method Using String.format() Method Using Byte Operation Using Integer.toHexString() Method It is a built-in function of the java.lang.Integer class. Syntax: public static String toHexString(int...
3 min read
Combining two sorted linked lists is one of the fundamental problems that must be solved when learning about algorithms. It is a process of uniting two sorted lists so that once integrated, the resulting list still remains sorted. This problem is usually presented as a coding challenge...
5 min read
Java SE 7 introduced major improvements to how errors are handled, bringing in features that make error management in Java applications simpler and more efficient. These changes aimed to improve code readability, decrease repetitive code (boilerplate), and enhance the overall experience for developers. The evolution of exception...
7 min read
The Shunting Yard algorithm is a commonly used algorithm in computer science for converting infix expressions to postfix or prefix expressions. In postfix notation, also known as Reverse Polish Notation (RPN), the operator is placed after the operands, while in prefix notation, also known as Polish...
8 min read
Difference between Array and ArrayList In Java, array and ArrayList are the well-known data structures. An array is a basic functionality provided by Java, whereas ArrayList is a class of Java Collections framework. It belongs to java.util package. Java Array An array is a dynamically-created object. It serves...
3 min read
? In Java, splitting string is an important and usually used operation when coding. Java provides multiple ways to split the string. But the most common way is to use the split() method of the String class. In this section, we will learn how to split a...
9 min read
Given an integer array (arr) and an integer target, we need to find the value closest to the target that can be obtained by applying the bitwise AND operation on a non-empty subarray of arr. The task is to return the minimum absolute difference between the...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India