DEV Community

Jayashree
Jayashree

Posted on

Mastering Java ArrayList: Important Methods Explained Simply

When you start working with Java, one of the first useful classes you’ll come across is ArrayList. At first, it may look similar to an array, but it solves a major limitation size.

Unlike arrays, an ArrayList can grow and shrink dynamically. You don’t need to worry about defining a fixed size in advance, which makes it very practical in real-world applications.

Let’s go through the most important methods in a simple and relatable way.

Getting Started

Here’s a basic example of creating and using an ArrayList:

package project.com;
import java.util.*;

public class ArraylistMethods {

    public static void main(String[] args) {

        ArrayList<Object> container = new ArrayList<>();

        container.add(10);
        container.add(30.4);
        container.add(true);
        container.add("hiii");
        container.add(10);
        container.add("hello");

        System.out.println(container);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the list stores different types of values. This works because we used Object as the type.

Adding Elements

  • Adding data is the most common operation. By default, elements are added to the end of the list.

container.add(100);

  • If needed, you can insert an element at a specific position:

container.add(2, "Java");

  • This shifts existing elements to the right.

Removing Elements

  • Removing elements can be done in two ways — using index or value.

container.remove(3);

  • This removes the element at index 3.

container.remove("hiii");

  • This removes the first occurrence of the given value.

Combining Lists

  • Sometimes you may need to combine two lists. That’s where addAll() comes in.
ArrayList<Object> container2 = new ArrayList<>();
container2.add(1);
container2.add(2);

container.addAll(container2);
Enter fullscreen mode Exit fullscreen mode
  • Now all elements from container2 are added to container.

Clearing the List

  • If you want to remove everything from a list:

container2.clear();

  • After this, the list will be empty.

Checking for Values

  • To check whether a value exists in the list:

container.contains(10);

  • This returns either true or false, which is useful for validations.

Accessing Elements

  • To retrieve a value using its index:

container.get(1);

  • This gives the element at index 1.

Finding the Size

  • To know how many elements are present:

container.size();

  • This is commonly used in loops and conditions.

Finding Element Positions

  • If you want to know where a value appears:

container.indexOf(10);
container.lastIndexOf(10);

  • The first method returns the first occurrence, while the second returns the last occurrence.

Checking if the List is Empty

  • Before performing operations, it’s sometimes useful to check if the list is empty:

container.isEmpty();

Updating Elements

  • To replace an existing value:

container.set(1, "Updated Value");

  • This updates the value at index 1.

Looping Through the List

  • There are multiple ways to iterate over an ArrayList.

Using an iterator:

Iterator<Object> it = container.iterator();

while(it.hasNext()) {
    System.out.println(it.next());
}
Enter fullscreen mode Exit fullscreen mode

Sorting and Reversing

  • You can also manipulate the order of elements.
Collections.sort(container);
Collections.reverse(container);
Enter fullscreen mode Exit fullscreen mode
  • Sorting works only when all elements are of the same type.

Comparing Two Lists

  • To check if two lists are equal:

container.equals(container2);

  • This compares both content and order.

A Small but Important Tip

  • Instead of using a raw ArrayList, it’s better to define a specific type.

ArrayList<Integer> list = new ArrayList<>();

  • This improves readability and prevents runtime errors.

Final Thoughts

ArrayList is one of the most commonly used data structures in Java. Once you understand these methods, working with collections becomes much easier.

Focus on practicing operations like adding, removing, accessing, and searching. These form the foundation for solving most real-world problems.

Top comments (0)