Skip to main content

Command Palette

Search for a command to run...

7) ArrayList In Java

A more flexible version of Array in Java

Updated
•3 min read•View as Markdown
7) ArrayList In Java
A

Product-oriented Software Engineer with a solid understanding of web programming fundamentals and software development methodologies such as agile and scrum.

An ArrayList in Java is part of the java.util package. It is a resizable array-like data structure that allows you to dynamically manage a collection of elements.

Importing the ArrayList Class

import java.util.ArrayList;

Declaring and Initializing

// ArrayList listName = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();

Adding Elements

Use the add() method to add elements.

ArrayList<String> names = new ArrayList<>();

names.add("Alice");
names.add("Bob");

System.out.println(names);  // Output: [Alice, Bob]

Accessing Elements

Use the get(index) method to access elements.

System.out.println(names.get(0));  // Output: Alice

Modifying Elements

Use the set(index, element) method to update an element.

ArrayList<String> names = new ArrayList<>();

names.set(0, "Alice");
names.set(1, "Charlie");

System.out.println(names);  // Output: [Alice, Charlie]

Removing Elements

1. By Index

names.remove(0);  // Removes the first element

2. By Value

names.remove("Charlie");  // Removes "Charlie"

Checking the Size

Use size() to get the number of elements.

System.out.println("Size: " + names.size());  // Output: Size: 1

Iterating Over

1. Using a for Loop

for (int i = 0; i < names.size(); i++) {
    System.out.println(names.get(i));
}

2. Using an Enhanced for Loop (for-each)

for (String name : names) {
    System.out.println(name);
}

3. Using an Iterator

import java.util.Iterator;

Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

Common Methods in ArrayList

Method Description

  • add(element): Adds an element to the end of the list.

  • add(index, element): Adds an element at the specified index.

  • get(index): Returns the element at the specified index.

  • set(index, element): Updates the element at the specified index.

  • remove(index): Removes the element at the specified index.

  • remove(element): Removes the first occurrence of the specified element.

  • size(): Returns the number of elements in the list.

  • clear(): Removes all elements from the list.

  • contains(element): Checks if the list contains the specified element.

  • isEmpty(): Returns true if the list is empty.

  • indexOf(element): Returns the index of the element.

Example: Complete Usage

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();

        // Adding elements
        colors.add("Red");
        colors.add("Blue");
        colors.add("Green");

        // Accessing elements
        System.out.println("First color: " + colors.get(0));  // Output: First color: Red

        // Modifying elements
        colors.set(1, "Yellow");

        // Iterating using a for-each loop
        for (String color : colors) {
            System.out.println(color);
        }

        // Checking size
        System.out.println("Total colors: " + colors.size());  // Output: Total colors: 3

        // Removing an element
        colors.remove("Green");
        System.out.println(colors);  // Output: [Red, Yellow]
    }
}

Difference Between Arrays and ArrayLists

Array

  • Fixed size (cannot change after creation).

  • Can store primitives and objects.

  • No built-in methods for operations.

ArrayList

  • Dynamic size (can grow and shrink).

  • Stores objects (Wrapper classes for primitives).

  • Provides built-in methods like add(), remove(), etc.

Learning Java

Part 8 of 26

In this series, I will break down the task of learning java into smaller actionable steps and work on them topic by topic while trying to provide observability into my learning process.

Up next

8) Loops In Java

The way of iterating in Java