Java ArrayList

Posted by Hayri Cicek on September 10, 2019


Java ArrayList are dynamic arrays that can grow as needed not like standard Java arrays which are fixed length.
The following statement will create an ArrayList.


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


Add elements to an ArrayList
We use add() method to add elements to an ArrayList.

import java.util.ArrayList;

public class AddElementsToArrayList {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        System.out.println("Swedish cities : " + cities);
    }
}


Output:

$ javac AddElementsToArrayList.java
$ java AddElementsToArrayList
Swedish cities : [Helsingborg, Malmö, Stockholm, Göteborg]


Remove elements from ArrayList
To remove elements from an ArrayList, we use the remove() method.

import java.util.ArrayList;

public class RemoveElementsFromArrayList {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");
        cities.add("Berlin");
        cities.add("London");

        System.out.println("Swedish cities : " + cities);

        // removing 5th element from the list
        cities.remove(5);

        // removing "Berlin" from the list
        cities.remove("Berlin");

        System.out.println("Swedish cities : " + cities);
    }
}


Output:

$ javac RemoveElementsFromArrayList.java
$ java RemoveElementsFromArrayList
Swedish cities : [Helsingborg, Malmö, Stockholm, Göteborg, Berlin, London]
Swedish cities : [Helsingborg, Malmö, Stockholm, Göteborg]


Iterating ArrayList
The following example shows how to iterate over an ArrayList using for-each loop.

import java.util.ArrayList;

public class IterateArrayListUsingForEachLoop {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        for (String city : cities) {
            System.out.println("City: " + city);
        }
    }
}


Output:

$ javac IterateArrayListUsingForEachLoop.java
$ java IterateArrayListUsingForEachLoop
City: Helsingborg
City: Malmö
City: Stockholm
City: Göteborg


Iterate using forEach and lambda expression.


import java.util.ArrayList;

public class IterateUsingForEachLambdaExample {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        cities.forEach(city -> {
            System.out.println("City " + city);
        });
    }
}


Output

java IterateUsingForEachLambdaExample
City Helsingborg
City Malmö
City Stockholm
City Göteborg


The following example shows how to iterate over an ArrayList using iterator() method.


import java.util.ArrayList;
import java.util.Iterator;

public class IterateArrayListUsingIterator {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        Iterator<String> cityIterator = cities.iterator();
        while(cityIterator.hasNext()) {
            String city = cityIterator.next();
            System.out.println("City " + city);
        }
    }
}


Output:

$ javac IterateArrayListUsingIterator.java
$ java IterateArrayListUsingIterator
City Helsingborg
City Malmö
City Stockholm
City Göteborg


In the last example, we will use iterator() and forEachRemaining() methods to iterate over ArrayList elements.


import java.util.ArrayList;
import java.util.Iterator;

public class IteratorAndForEachRemainingExample {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        Iterator<String> cityIterator = cities.iterator();
        cityIterator.forEachRemaining(city -> {
            System.out.println("City " + city);
        });
    }
}


Output:

$ javac IteratorAndForEachRemainingExample.java
$ java IteratorAndForEachRemainingExample
City Helsingborg
City Malmö
City Stockholm
City Göteborg


Share this: