DEV Community

Cover image for Mastering Java ArrayLists: A Comprehensive Guide πŸ“šβœ¨
Dhanush Kumar
Dhanush Kumar

Posted on

Mastering Java ArrayLists: A Comprehensive Guide πŸ“šβœ¨

Introduction

In Java, a List is an ordered collection (also known as a sequence) that allows duplicates and provides a way to access elements by their integer index. An ArrayList is a resizable array implementation of the List interface in Java. It allows you to store a dynamic collection of elements.

Key Characteristics of ArrayLists:

  • Ordered: Elements in a list have a specific order, and you can access them by their index (starting from 0).

  • Allows Duplicates: Lists can contain multiple instances of the same object.

Core ArrayList Implementations

An ArrayList is a resizable array implementation of the List interface in Java. It provides a way to store a dynamic collection of elements that can grow and shrink in size as needed. ArrayList in java pakage import java.util.ArrayList;

  • Dynamic Sizing: Unlike arrays, ArrayList can automatically resize itself when elements are added or removed.
  • Order of Elements: Maintains the order in which elements are added.
  • Allows Duplicates: You can store duplicate elements.
  • Random Access: Provides fast random access to elements using the get(int index) method.
  • Performance:
    • Fast for retrieval O(1) time complexity.
    • Slower for adding/removing elements from the middle (O(n) time complexity) because it may require shifting elements.

ArrayListExample.java

Here's the complete code example that we will be discussing below:

import java.util.ArrayList;

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

        // Adding elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Mango");

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

        // Accessing an element
        String secondFruit = fruits.get(1); // Index starts from 0
        System.out.println("Second fruit: " + secondFruit); // Output: Banana

        // Removing an element
        fruits.remove("Mango"); // Remove by value
        System.out.println("After removing Mango: " + fruits);

        // Size of the ArrayList
        System.out.println("Number of fruits: " + fruits.size());

        // Iterating through the ArrayList
        System.out.println("All fruits:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Output of ArrayListExample.java:

Fruits: [Apple, Banana, Cherry, Mango]
Second fruit: Banana
After removing Mango: [Apple, Banana, Cherry]
Number of fruits: 3
All fruits:
Apple
Banana
Cherry

Enter fullscreen mode Exit fullscreen mode

1. Importing the ArrayList Class

import java.util.ArrayList;
Enter fullscreen mode Exit fullscreen mode

Purpose: This line imports the ArrayList class from the java.util package, which is necessary to use ArrayList in our program.

2. Creating an ArrayList

ArrayList<String> fruits = new ArrayList<>();
Enter fullscreen mode Exit fullscreen mode

Purpose: This line creates an ArrayList named fruits that will store String elements. The angle brackets <String> specify the type of elements the list will hold.

3. Adding Elements to the ArrayList

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Mango");
Enter fullscreen mode Exit fullscreen mode

Purpose: These lines add four fruit names to the fruits ArrayList. The add method appends the specified element to the end of the list.

4. Printing the ArrayList

System.out.println("Fruits: " + fruits);
Enter fullscreen mode Exit fullscreen mode

Purpose: This line prints the entire ArrayList. The toString method of ArrayList is automatically called, displaying the contents in square brackets.

5. Accessing an Element

String secondFruit = fruits.get(1);
Enter fullscreen mode Exit fullscreen mode

Purpose: This line retrieves the second element from the fruits ArrayList (index 1, since indexing starts at 0). The value is stored in the variable secondFruit.

6. Removing an Element

fruits.remove("Mango");
Enter fullscreen mode Exit fullscreen mode

Purpose: This line removes the element "Mango" from the fruits ArrayList. The remove method can take either an index or an object to remove.

7. Getting the Size of the ArrayList

System.out.println("Number of fruits: " + fruits.size());
Enter fullscreen mode Exit fullscreen mode

Purpose: This line prints the number of elements currently in the fruits ArrayList using the size method.

8. Iterating Through the ArrayList

for (String fruit : fruits) {
    System.out.println(fruit);
}
Enter fullscreen mode Exit fullscreen mode

Purpose: This for-each loop iterates over each element in the fruits ArrayList and prints each fruit on a new line.

Conclusion

This guide provides a step-by-step breakdown of how to use an ArrayList in Java. You learned how to create an ArrayList, add and remove elements, access specific items, and iterate through the list. Understanding these basic operations will help you work with collections in Java effectively.

Top comments (0)