ArrayList in Java Explained with Example (Beginner Guide)

ArrayList in Java Explained with Example (Beginner Guide)

If you're learning Java, understanding ArrayList is very important. It is one of the most commonly used classes in Java and is part of the Collection Framework.

When I first learned ArrayList, I was confused about when to use it instead of arrays. But once I practiced with examples, it became much easier.

Written by Shivkumar Udas – Engineering student.


๐Ÿ’ก What is ArrayList in Java?

ArrayList is a dynamic array that can grow and shrink in size automatically.

Unlike normal arrays, you don’t need to define the size in advance.


๐Ÿ“Œ Why Use ArrayList?

  • Resizable (no fixed size)
  • Easy to add and remove elements
  • Part of Java Collections Framework

๐Ÿ“Š Array vs ArrayList

Arrays have a fixed size, meaning once created, their size cannot change. In contrast, ArrayList can grow and shrink dynamically, making it more flexible.

Use arrays when size is fixed. Use ArrayList when size can change frequently.


๐Ÿ’ก When Should You Use ArrayList?

Use ArrayList when you don’t know how many elements you need to store or when your data changes frequently.

It is useful when you need flexible data storage and easy operations like adding or removing elements.


๐Ÿ“Œ How to Create an ArrayList


import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
    }
}

This creates an empty ArrayList that can store strings.


๐Ÿ“Œ Adding Elements


list.add("Java");
list.add("Python");
list.add("C++");

๐Ÿ“Œ Important Methods Summary

  • add() – Adds element
  • get() – Access element
  • set() – Update element
  • remove() – Delete element
  • size() – Get total elements

The add() method is used to insert elements.


๐Ÿ“Œ Accessing Elements


System.out.println(list.get(0));

The get() method is used to access elements using index.


๐Ÿ“Œ Updating Elements


list.set(0, "Java Updated");

The set() method updates an element at a specific index.


๐Ÿ“Œ Removing Elements


list.remove("Python");

This removes the specified element from the list.


๐Ÿ“Œ Looping Through ArrayList


for(String item : list){
    System.out.println(item);
}

This loop prints all elements in the list.


๐Ÿ” Real-Life Example

Imagine you are storing names of students in a class. The number of students can change, so using an ArrayList is better than a fixed-size array.


⚠️ Common Mistakes

  • Trying to access an index that doesn’t exist
  • Forgetting to import ArrayList

๐Ÿงช Practice Task

Create an ArrayList of integers and add 5 numbers. Then print all numbers using a loop.


๐Ÿ“Œ What to Learn Next

After ArrayList, you can learn other collection classes like HashMap and LinkedList to improve your Java skills.


๐ŸŽฏ Conclusion

ArrayList is a powerful and flexible data structure in Java. Once you understand how to use it, working with data becomes much easier.

Practice regularly and try different examples to build confidence.

Comments