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
Parameter | Description |
---|---|
index | Index of the element to retrieve (0-based) |
- The index must be between
0
andsize() - 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, soget()
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 likeLinkedList
.
Summary
Aspect | Details |
---|---|
Method Purpose | Access element at a specific index in ArrayList |
Syntax | list.get(int index) |
Return Value | Element at the given index (type E ) |
Index Base | Zero-based (first element at 0) |
Throws Exception | IndexOutOfBoundsException if index is invalid |
Typical Use Cases | Reading 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.