In this tutorial, we will learn about the Java ArrayList get() method, and learn how to use this method to get the element in this ArrayList at specified position, with the help of examples.
Java ArrayList get() method
ArrayList get() returns the element at the specified position in this ArrayList.
Syntax
The syntax of get() method with index as argument is
ArrayList.get(int index)
where
Parameter | Description |
---|---|
index | The index of the element which we would like to get from the ArrayList. |
Returns
The method returns the element of the type present in ArrayList.
1. get(index) – Get element at given index in ArrayList
In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList get() method to get the elements at index 0, 1, 2 and 3 and print them to console.
Java Program
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
arrayList.add("e");
System.out.println(arrayList.get(0));
System.out.println(arrayList.get(1));
System.out.println(arrayList.get(2));
System.out.println(arrayList.get(3));
}
}
Output
a
b
c
d
2. get(index) – ArrayList of User-defined Objects
In this example, we will define an ArrayList that can store objects of type Car
. We will use ArrayList get() method to get the element at index 1
and print its properties to the console.
Java Program
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<Car> arrayList = new ArrayList<Car>();
arrayList.add(new Car(1, "Tesla"));
arrayList.add(new Car(2, "BMW"));
arrayList.add(new Car(3, "Toyota"));
Car car = arrayList.get(1);
System.out.println("The car id is : " + car.id);
System.out.println("The car name is : " + car.name);
}
}
class Car {
int id;
String name;
public Car(int id, String name) {
this.id = id;
this.name = name;
}
}
Output
The car id is : 2
The car name is : BMW
3. get(index) – java.lang.IndexOutOfBoundsException
In this example, we will create and initialize an ArrayList with five elements. We will try to access an element at index 14
. Since, the index is out of bounds for the ArrayList, get() method throws java.lang.IndexOutOfBoundsException.
Java Program
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
arrayList.add("e");
System.out.println(arrayList.get(14));
}
}
Output
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 14 out-of-bounds for length 5
at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
at java.base/java.util.Objects.checkIndex(Unknown Source)
at java.base/java.util.ArrayList.get(Unknown Source)
at Example.main(Example.java:12)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java ArrayList get() method, and also learnt how to use this method with the help of Java example programs.