ArrayList
📚 Java ArrayList — How I Finally “Got It” (And Why It’s So Cool)
I’ve been learning about Java Collections, and today I had a click moment with ArrayList
.
So I thought I’d break it down the way it made sense to me.
If you’re also learning Java, this might save you hours of confusion 😄
🧠 First, what’s the big deal with ArrayList?
Let’s say you want to store the marks of 5 students.
You’d probably do this:
int[] marks = new int[5];
Great. But what if you don’t know how many students there are in advance?
Arrays are fixed-size, so you’re stuck.
That’s when you learn about ArrayList — a magical list that grows and shrinks dynamically.
👉 How to Define an ArrayList:
ArrayList<Integer> numbers = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
🧠 But Wait... What's That <Integer>
or <String>
in ArrayList<Integer>
?
That’s because ArrayList is a generic class in Java.
ArrayList<Integer> list = new ArrayList<>();
Here’s the breakdown:
- Java collections like
ArrayList
are generic, which means you can specify what type of data they will store. - The
< >
part is called a type parameter. It helps the compiler know what kind of object this list will hold — and prevents errors.
For example:
ArrayList<String> names = new ArrayList<>();
This means the list will only store Strings. If you try to add anything else (like an Integer), Java will throw an error.
✅ Why use Generics?
- To make your code type-safe
- To avoid type casting
- To catch errors at compile time, not at runtime
✅Inserting values in an ArrayList
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
Unlike arrays, you don't need to define the size.
It expands as you add elements! 🤯
🔍 Accessing and Searching
System.out.println(list.get(1)); // 👉 20
System.out.println(list.contains(30)); // 👉 true
Use .get(index)
to access elements, and .contains()
to check if an element exists.
❌ Removing Elements
list.remove(2); // removes element at index 2
But here's something I didn’t understand at first:
list.remove(1); // removes element at index 1 ❗
list.remove(Integer.valueOf(10)); // removes the *value* 10 ✅
⚠️ Java has two remove() methods — one takes an index, the other takes an object.
✨ Inserting & Updating
list.add(1, 100); // inserts 100 at index 1
list.set(0, 500); // replaces value at index 0 with 500
You can insert at any position, not just at the end!
📦 Converting to Array
Integer[] array = list.toArray(new Integer[0]);
System.out.println(Arrays.toString(array));
The toArray(new Integer[0])
part tells Java to auto-size the array.
(It’s a Java convention — just roll with it 😄)
🧠 What’s Happening Internally?
- The initial capacity of an ArrayList is 10, even though the size is 0.
- When full, it resizes (usually 1.5x bigger), which takes time.
- You can set initial capacity if needed:
ArrayList<Integer> bigList = new ArrayList<>(1000);
If you know you’ll store a lot of items — this saves memory and improves performance. 🚀
🧹 Sorting the List
list.add(23);
list.add(12);
list.add(45);
Collections.sort(list); // ascending sort
list.sort(null); // also works, uses natural order
Both do the same job — but .sort(null)
works directly on the list object.
🎯 Real-World Analogy That Helped Me
ArrayList felt confusing at first, but here's how I visualized it:
Think of an ArrayList like a dynamic drawer —
You can keep adding or removing things without worrying about space, and it auto-organizes when needed.
Sure! Here's your updated version with the new channel mention and a revised conclusion:
🔚 Conclusion
Arrays = fixed size 😐
ArrayList = dynamic, flexible, and beginner-friendly 💡
But be careful with remove()
— it can remove by index or value, and that can trip you up at first. Once you get it, though, it’s super useful!
🗨️ I’ve been learning Java Collections through the Engineering Digest YouTube channel, and it’s been a game-changer in understanding how things actually work under the hood.
Sharing this as part of my Java learning journey. If you're on the same path, let’s connect and grow together! 🚀💻
Top comments (0)