Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ArrayList get() method
Syntax and Examples


Introduction

The get() method in Java's ArrayList allows you to access elements stored within the list. Think of an ArrayList like a numbered shelf where you can store objects. The get() method is how you retrieve a specific object from its assigned number (index) on that shelf.

Syntax


public E get(int index)

Parameters

Parameter Description
index The index of the element to retrieve. The index starts from 0.

Return Value

The get() method returns the element at the specified index.

Examples

Example 1: Accessing a String Element

This example demonstrates how to retrieve a string from an ArrayList. We create an ArrayList of strings and then use get() to access the element at index 1.


import java.util.ArrayList;

public class GetExample {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        // Access the element at index 1 (which is "Bob")
        String nameAtIndex1 = names.get(1);

        System.out.println(nameAtIndex1); // Output: Bob
    }

Bob

Explanation: We create an ArrayList called names and add three strings to it. Then, we call names.get(1) to retrieve the element at index 1. Remember that array indexes start from 0, so index 1 refers to the second element in the list.

Example 2: Accessing an Integer Element

This example shows how to access elements of type integer within an ArrayList. We create an ArrayList of integers and retrieve the value at a specified index.


import java.util.ArrayList;

public class GetIntegerExample {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        // Access the element at index 0 (which is 10)
        int numberAtIndex0 = numbers.get(0);

        System.out.println(numberAtIndex0); // Output: 10
    }

10

Explanation: Similar to the previous example, this demonstrates accessing an element in an ArrayList but this time we are using integers. We add three integer values and then access the one at index 0.

Example 3: Handling Index Out of Bounds Exception

This example highlights what happens when you try to access an index that doesn't exist in the ArrayList (i.e., attempting to retrieve an element beyond the valid range). This results in an IndexOutOfBoundsException.


import java.util.ArrayList;

public class GetOutOfBoundExample {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");

        try {
            // Attempting to access index 2, which is out of bounds.
            String colorAtIndex2 = colors.get(2);  // This will throw an exception
            System.out.println(colorAtIndex2);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: Index out of bounds!" + e.getMessage());
        }
    }

Error: Index out of bounds! Index 2 is out of bounds for length 2

Explanation: The ArrayList colors has only two elements (at indices 0 and 1). Attempting to access index 2 results in an IndexOutOfBoundsException because that index doesn't exist. The try-catch block handles this exception gracefully by printing an error message instead of crashing the program.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M