Java ArrayList
An ArrayList is a list that grows and shrinks as you add or remove items. A normal array has a fixed size. An ArrayList removes that limit and offers many ready-made methods.
A Shopping Bag Example
A paper bag with fixed slots holds exactly five items. A stretchy cloth bag holds as many items as you carry home. An array behaves like the paper bag. An ArrayList behaves like the cloth bag.
Diagram: Array and ArrayList
Array (size fixed at 4)
[ A ][ B ][ C ][ D ] cannot add a fifth item
ArrayList (size changes)
[ A ][ B ][ C ][ D ]
add("E")
[ A ][ B ][ C ][ D ][ E ] grows by itself
Creating an ArrayList
Import the class from java.util. Put the item type inside angle brackets. Use wrapper types such as Integer for numbers.
import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
ArrayList<Integer> marks = new ArrayList<>();
Everyday Methods
Adding Items
fruits.add("Apple");
fruits.add("Banana");
fruits.add(1, "Mango"); // insert at position 1
System.out.println(fruits); // [Apple, Mango, Banana]
Reading and Changing Items
System.out.println(fruits.get(0)); // Apple
fruits.set(2, "Grapes");
System.out.println(fruits); // [Apple, Mango, Grapes]
Removing Items
fruits.remove("Apple"); // remove by value
fruits.remove(0); // remove by position
Size and Search
System.out.println(fruits.size());
System.out.println(fruits.contains("Grapes"));
System.out.println(fruits.indexOf("Grapes"));
System.out.println(fruits.isEmpty());
Method Table
| Method | Job |
|---|---|
| add(item) | Adds at the end |
| add(index, item) | Inserts at a position |
| get(index) | Reads one item |
| set(index, item) | Replaces one item |
| remove(x) | Deletes by value or position |
| size() | Counts items |
| clear() | Removes everything |
Looping Through an ArrayList
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
for (String f : fruits) {
System.out.println(f);
}
Sorting
import java.util.Collections;
ArrayList<Integer> nums = new ArrayList<>();
nums.add(30);
nums.add(10);
nums.add(20);
Collections.sort(nums);
System.out.println(nums); // [10, 20, 30]
How Growth Works
An ArrayList stores items in a hidden array. When the hidden array fills up, Java builds a larger array and copies every item across. You never see this work, and the list keeps behaving like one smooth container.
hidden array full: [ A ][ B ][ C ][ D ]
|
v build bigger array, copy items
new array: [ A ][ B ][ C ][ D ][ ][ ]
Real Example: Student Names
A class teacher records names as students arrive. The number of students changes every day, so the teacher uses an ArrayList.
ArrayList<String> present = new ArrayList<>();
present.add("Aman");
present.add("Sara");
present.add("Dev");
System.out.println("Total present: " + present.size());
ArrayList and Array Compared
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed | Flexible |
| Item types | Primitives and objects | Objects only |
| Ready methods | Few | Many |
Common Mistakes
- Calling remove(1) on an Integer list and removing position 1 when you wanted the value 1
- Reading an index that does not exist, which throws an IndexOutOfBoundsException
- Changing the list inside a for-each loop, which throws a ConcurrentModificationException
- Forgetting the import line
Copying and Joining Lists
Create a copy by passing one list to the constructor of another. The copy holds the same items and lives as a separate list, so changes in one list leave the other list untouched.
ArrayList<String> backup = new ArrayList<>(fruits);
backup.addAll(fruits); // joins all items at the end
Starting Capacity
Give the list a starting capacity when you already know the approximate size. A good guess avoids repeated growth and saves time on large lists.
ArrayList<Integer> big = new ArrayList<>(1000);
Quick Summary
- ArrayList resizes on its own
- add, get, set, remove, and size cover most work
- Use wrapper types for numbers
