ArrayList in Java is a resizable array implementation from the java.util package, part of the Java Collections Framework. It dynamically adjusts its size as elements are added or removed, unlike fixed-size arrays.
Key Features
Dynamic Size: Grows and shrinks automatically when elements are added or removed.
Ordered Storage: Maintains insertion order; elements can be accessed by index (0-based).
Duplicate Elements: Allows duplicate values.
Null Values: Can store
nullelements.Generic Type Safety: Supports generics (e.g.,
ArrayList<String>) for type safety at compile time.Built-in Methods: Provides methods like
add(),remove(),get(),set(),size(),contains(), andclear()for easy manipulation.
Basic Usage
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> cars = new ArrayList<>();
// Add elements
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
// Access element
System.out.println(cars.get(0)); // Output: Volvo
// Modify element
cars.set(0, "Opel");
// Remove element
cars.remove(0);
// Check size
System.out.println(cars.size()); // Output: 2
// Loop through elements
for (String car : cars) {
System.out.println(car);
}
}
}Performance & Trade-offs
Fast Access:
get()andset()operations are O(1).Amortized Fast Add:
add()is O(1) on average.Slower Middle Insert/Remove: Inserting or removing from the middle is O(n) due to shifting elements.
Memory Overhead: Slightly more memory than arrays due to internal array management.
When to Use
When the number of elements is unknown or changes dynamically.
When frequent additions, removals, or modifications are needed.
When you need built-in methods for searching, sorting, or iteration.
Note: For better performance with frequent
contains,add, orremoveoperations, consider usingHashSetorLinkedHashSetinstead.
You can use the following instruction:
new ArrayList<>(Arrays.asList(array));
Answer from Tom on Stack Overflowjava - Create ArrayList from array - Stack Overflow
Java: Is Arraylist better than Arrays?
Is using ArrayList good practice for a regular software engineering job?
How do I create a 2d ArrayList in java?
Videos
You can use the following instruction:
new ArrayList<>(Arrays.asList(array));
Given:
Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) };
The simplest answer is to do:
List<Element> list = Arrays.asList(array);
This will work fine. But some caveats:
- The list returned from asList has fixed size. So, if you want to be able to add or remove elements from the returned list in your code, you'll need to wrap it in a new
ArrayList. Otherwise you'll get anUnsupportedOperationException. - The list returned from
asList()is backed by the original array. If you modify the original array, the list will be modified as well. This may be surprising.
I've been programming in Java for 10 months now (as a subject in school) and i was always curious whats the biggest difference between Areays and Arraylist. I know that if i had an Arraylist named 'list' and i wrote System.out.print(list) it will print all the things that the list contains. Something that Arrays can't do that easily. But whats the actually biggest difference?
Everytime I do LeetCode or codewars problems that contain array manipulations they never use ArrayList. How often do software engineers use ArrayList? Is it useful? Is it practical?