GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — ArrayList get(index) Method in Java with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

ArrayList get(index) Method in Java with Examples

Last Updated : 22 Jul, 2026

The get(int index) method of the ArrayList class is used to retrieve the element present at the specified index in the list. Since ArrayList stores elements in insertion order and supports random access, the get() method provides fast retrieval of elements by their position.

  • Retrieves an element using its zero-based index.
  • Provides constant-time O(1) random access to elements.
Java
import java.util.ArrayList;

public class GFG {
    public static void main(String[] args) {

        // Create an ArrayList of Strings
        ArrayList<String> fruits = new ArrayList<>();

        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");

        // Retrieve the element at index 1
        String fruit = fruits.get(1);

        System.out.println("Element at index 1: " + fruit);
    }
}

Output
Element at index 1: Banana

Explanation: In the above example, an ArrayList containing three fruits is created. The get(1) method returns the element present at index 1, which is Banana.

Syntax

public E get(int index)

  • Parameter: index: The zero-based position of the element to retrieve.
  • Return Value: Returns the element present at the specified index.
  • Exception: IndexOutOfBoundsException: Thrown if the specified index is less than 0 or greater than or equal to the size of the ArrayList.

Example: Accessing Elements Using get() Inside a Loop

Java
import java.util.ArrayList;

public class GFG {
    public static void main(String[] args) {

        // Create an ArrayList of Integers
        ArrayList<Integer> numbers = new ArrayList<>();

        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.add(40);

        // Access each element using get()
        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
Element at index 3 = 40

Explanation: Here, the get() method is used inside a loop to retrieve each element from the ArrayList using its index.

Example: Handling Invalid Index

Java
import java.util.ArrayList;

public class GFG {
    public static void main(String[] args) {

        // Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();

        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        // Attempt to access an invalid index
        System.out.println(numbers.get(5));
    }
}

Output: 

Exception in thread "main"
java.lang.IndexOutOfBoundsException:
Index 5 out of bounds for length 3

Explanation: The ArrayList contains only 3 elements (indices 0 to 2). Attempting to access index 5 results in an IndexOutOfBoundsException.

get() Vs elementAt()

get() (ArrayList)elementAt() (Vector)
Belongs to ArrayList.Belongs to Vector.
Not synchronized.Synchronized (thread-safe).
Provides faster performance.Slightly slower due to synchronization.
Used with the Collections Framework.Used with the legacy Vector class.

Advantages of get() Method

  • Retrieves elements directly using their index.
  • Offers O(1) time complexity for element access.
  • Preserves the original ArrayList without modification.
  • Simple and efficient for random access.
  • Works with any object type stored in an ArrayList.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.