Home Java Tutorial ArrayList in Java

ArrayList in Java

by Team Impactmillions
61 minutes read

In this tutorial, you’ll learn about ArrayList in Java, Key Features of ArrayList, Importing ArrayList, Creating an ArrayList, Modifying Elements in an ArrayList, Removing Elements from an ArrayList, Iterating Over an ArrayList along with examples.

  • ArrayList is a part of the Java Collections Framework and is one of the most commonly used classes in Java for storing dynamic lists of objects.
  • Unlike arrays, ArrayList can grow and shrink in size dynamically.
  • It provides more flexibility compared to arrays due to its ability to handle variable-sized collections.

Key Features of ArrayList

  • Dynamic Size: The size of an ArrayList can increase or decrease dynamically as elements are added or removed.
  • Homogeneous Elements: Typically, ArrayList holds elements of a single type, specified when the ArrayList is declared.
  • Indexed Access: Elements can be accessed using their index, just like arrays.
  • Provides Utility Methods: ArrayList comes with a set of useful methods for manipulation, such as adding, removing, and searching for elements.

Importing ArrayList

To use ArrayList, you need to import it from the java.util package.

import java.util.ArrayList;

Creating an ArrayList

You can create an ArrayList using its constructor. You can specify the type of elements it will hold using generics.

ArrayList<String> stringList = new ArrayList<>();
ArrayList<Integer> intList = new ArrayList<>();

Adding Elements to an ArrayList

You can add elements to an ArrayList using the add method.

stringList.add("Hello");
stringList.add("World");
intList.add(10);
intList.add(20);

Accessing Elements in an ArrayList

You can access elements using the get method, which takes the index of the element as an argument.

String firstString = stringList.get(0);
int firstInt = intList.get(0);

System.out.println("First String: " + firstString); // Outputs: First String: Hello
System.out.println("First Integer: " + firstInt); // Outputs: First Integer: 10

Modifying Elements in an ArrayList

You can modify elements using the set method, which takes the index and the new value.

stringList.set(0, "Hi");
System.out.println("Modified First String: " + stringList.get(0));  
// Outputs: Modified First String: Hi

Removing Elements from an ArrayList

You can remove elements using the remove method, which can take either the index of the element or the element itself.

stringList.remove(1); // Removes the element at index 1
intList.remove(Integer.valueOf(20)); // Removes the element 20

Iterating Over an ArrayList

You can iterate over an ArrayList using a for loop, an enhanced for loop, or an iterator.

Using a For Loop

stringList.remove(1); // Removes the element at index 1
intList.remove(Integer.valueOf(20)); // Removes the element 20

Using an Enhanced For Loop

for (String element : stringList) {
    System.out.println("Element: " + element);
}

Example

Here’s a complete example demonstrating the creation, manipulation, and iteration of an ArrayList in Java:

import java.util.ArrayList;

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

        // Add elements to the ArrayList
        stringList.add("Hello");
        stringList.add("World");

        // Access elements
        String firstString = stringList.get(0);
        System.out.println("First String: " + firstString); // Outputs: First String: Hello

        // Modify an element
        stringList.set(0, "Hi");
        System.out.println("Modified First String: " + stringList.get(0)); // Outputs: Modified First String: Hi

        // Remove an element
        stringList.remove(1); // Removes the element at index 1

        // Iterate over the ArrayList
        for (String element : stringList) {
            System.out.println("Element: " + element); // Output: Element: Hi
        }
    }
}

Example: Calculating the Sum of ArrayList Elements:

Let’s create a simple program that calculates the sum of elements in an ArrayList:

import java.util.ArrayList;

public class ArrayListSum {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        // Calculate the sum
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }

        // Print the result
        System.out.println("Sum of elements: " + sum);   // Output: Sum of elements: 6
    }
}

related posts

Leave a Comment

Enable Notifications OK No thanks