Skip to content

Java ArrayList

ArrayList a dynamic list that can grow and shrink as needed, allowing you to store and manipulate collections of objects with ease. Unlike arrays, ArrayLists in Java offer flexibility and convenience, making them a popular choice for handling collections of data in your programs.

Let's start by creating an ArrayList and adding some elements to it:

java
import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();

        // Add some fruits to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Print the ArrayList
        System.out.println("Fruits: " + fruits);
    }
}

Isn't that cool? With just a few lines of code, we've created an ArrayList called fruits and added some juicy fruits to it. And the best part? ArrayLists automatically handle resizing and memory management for us, so we can focus on enjoying our fruits without worrying about the technical details.

But wait, there's more to ArrayLists than just adding elements! Let's explore some of their other features:

Accessing Elements

java
// Accessing elements by index
public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();

        // Add some fruits to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        String firstFruit = fruits.get(0);
        System.out.println("First fruit: " + firstFruit);
    }
}

ArrayLists allow you to access elements by their index, just like arrays. By using the get() method and providing the index of the element you want to retrieve, you can easily fetch specific items from your list.

Removing Elements

java
public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();

        // Add some fruits to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Removing elements by value
        fruits.remove("Banana");
        System.out.println("Fruits after removing Banana: " + fruits);
    }
}

Removing elements from an ArrayList is a breeze. You can simply call the remove() method and pass the value of the element you want to remove. The ArrayList will automatically adjust its size and reorganize its elements, making it seamless to manage your collection.

Checking Size

java
public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();

        // Add some fruits to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Checking the size of the ArrayList
        int size = fruits.size();
        System.out.println("Number of fruits: " + size);
    }
}

It's essential to know the size of your ArrayList, especially when working with dynamic collections. The size() method returns the number of elements currently stored in the ArrayList, allowing you to perform size-related operations and optimizations as needed.

Iterating Over Elements

java
public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();

        // Add some fruits to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Iterating over elements using a for-each loop
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

ArrayLists support easy iteration through their elements using enhanced for loops. This simplifies the process of traversing through your collection, allowing you to perform actions on each element without the complexity of managing indices manually.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.