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.
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
0or greater than or equal to the size of the ArrayList.
Example: Accessing Elements Using get() Inside a Loop
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
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.