Java Notes
Learn ArrayList in Java — creating and using ArrayLists, all essential methods, how ArrayList grows internally, sorting, searching, converting to array, and common mistakes.
ArrayList is Java's go-to resizable list. Internally it stores elements in an array, but when that array fills up, it automatically creates a bigger one and copies everything over. From the outside, it just grows as you add elements.
Use ArrayList when you need an ordered list, allow duplicates, and mostly access elements by index. It is the default choice for list data in Java.
Creating an ArrayList
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Core Methods
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
nums.add(1, 15)— inserts 15 at index 1. Elements from index 1 onwards shift right.nums.remove(Integer.valueOf(15))— removes by value. WithoutvalueOf,nums.remove(15)would be treated asremove(int index)— a different method!nums.remove(0)— removes element at index 0.- Both
removeforms exist: one takes anint(index), one takes anObject(value).
Iterating an ArrayList
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Sorting
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Sorting Custom Objects
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Converting ArrayList to Array and Back
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Note: Arrays.asList(array) returns a fixed-size list. Wrap it in new ArrayList<>(...) if you need to add or remove elements.
How ArrayList Grows Internally
ArrayList starts with a default internal array of 10 slots. When it fills up, Java creates a new array 1.5x the size and copies all elements.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
If you know you will store ~1000 elements, create new ArrayList<>(1000). This avoids multiple resize operations.
Common Mistake 1: remove(int) vs remove(Object)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
When the list holds Integer, list.remove(1) removes by index (not value 1). To remove by value, use remove(Integer.valueOf(value)).
Common Mistake 2: Modifying During For-Each
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Common Mistake 3: Storing Primitives Directly
// ArrayList<int> numbers = new ArrayList<>(); // COMPILE ERROR
ArrayList<Integer> numbers = new ArrayList<>(); // use Integer wrapper
numbers.add(5); // autoboxing: int 5 → Integer 5
int value = numbers.get(0); // unboxing: Integer 5 → int 5Practice Task
Task: Write a program that manages a todo list using ArrayList<String>. Implement:
- Add 5 tasks
- Mark a task as done by removing it by value
- List all remaining tasks with index numbers (1-based)
- Sort the remaining tasks alphabetically
- Check if a specific task exists
Hint: Use a for loop with i + 1 for 1-based indexing. Use Collections.sort() for alphabetical order. Use contains() to check.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: remove("Call dentist") removes by value. Index-based printing with i + 1 gives a natural 1-based list. After sorting, the order changes — notice the list is truly reordered, not just displayed differently.
Continue the lesson path