Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList get() Method

Posted on July 23, 2025July 23, 2025 By Admin No Comments on Java ArrayList get() Method

The get() method is a fundamental operation of the Java ArrayList class. It allows you to access elements by their index efficiently, making it a critical part of ordered, index-based data handling in Java. Understanding how get() works is essential for reading or using data stored in an ArrayList.

What Is the get() Method in ArrayList?

  • The get() method retrieves the element at a specific index in the list.
  • It returns the element stored at the given position.
  • The index is zero-based (the first element is at index 0).
  • You can use get() in a loop to access or display all elements.

Syntax

public E get(int index)

Where:

  • E is the data type of the elements stored in the list.
  • index is the position of the item you want to access (starts from 0).

Parameters

ParameterDescription
indexIndex of the element to retrieve (0-based)
  • The index must be between 0 and size() - 1.

Return Value

  • Returns the element at the specified position in the ArrayList.
  • The data type returned matches the list’s declared type.

Exceptions

  • IndexOutOfBoundsException: Thrown if the specified index is out of range (less than 0 or greater than or equal to the list size).
  • There are no other checked exceptions.

How Does get() Work Internally?

  • ArrayList is backed by an internal array, so get() operates in constant time O(1).
  • The method simply returns the element at the specified index.
  • Internally, it checks if the index is valid; if not, it throws an exception.
  • Efficient for both reading and iterating through lists.

Examples of the get() Method

1. Get Elements from an ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>();
        cities.add("Delhi");
        cities.add("Mumbai");
        cities.add("Chennai");

        // Gets the element at index 1 ("Mumbai")
        String city = cities.get(1); 
        System.out.println("City at index 1: " + city);
    }
}

Output:

City at index 1: Mumbai

2. Using get() in a Loop

import java.util.ArrayList;

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

        for (int i = 0; i < numbers.size(); i++) {
            System.out.println("Element at index " + i + ": " + numbers.get(i));
        }
    }
}

Output:

Element at index 0: 10
Element at index 1: 20
Element at index 2: 30

3. Handling IndexOutOfBoundsException

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");

        try {

            // Index 2 is out of bounds (size is 2)
            System.out.println(list.get(2)); 
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: Index 2 out of bounds for length 2

Important Notes

  • Indexing starts from 0 (not 1).
  • Always ensure you are accessing a valid index within the list’s current size().
  • If you call get() with an invalid index, your program will throw an exception and may stop execution.
  • Use get() when you need random access or want to read by position.
  • For performance, get() is much more efficient than in linked structures like LinkedList.

Summary

AspectDetails
Method PurposeAccess element at a specific index in ArrayList
Syntaxlist.get(int index)
Return ValueElement at the given index (type E)
Index BaseZero-based (first element at 0)
Throws ExceptionIndexOutOfBoundsException if index is invalid
Typical Use CasesReading by position, iteration, random access

Typical Use Cases

  • Accessing or displaying items by index.
  • Retrieving data in loops.
  • Reading user-selected elements from lists.
  • Implementing algorithms that need random or indexed access to elements.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList forEach() Method
Next Post: Java ArrayList indexOf() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme