DEV Community

Cover image for Collections - JAVA(Part A)
Abhijeet Vishwakarma
Abhijeet Vishwakarma

Posted on

Collections - JAVA(Part A)

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];
Enter fullscreen mode Exit fullscreen mode

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<>();
Enter fullscreen mode Exit fullscreen mode

🧠 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<>();
Enter fullscreen mode Exit fullscreen mode

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<>();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Use .get(index) to access elements, and .contains() to check if an element exists.


❌ Removing Elements

list.remove(2); // removes element at index 2
Enter fullscreen mode Exit fullscreen mode

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 ✅
Enter fullscreen mode Exit fullscreen mode

⚠️ 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
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)