Get Element from ArrayList in Java
To get an element from ArrayList in Java, call get() method on this ArrayList. get() method takes index as an argument and returns the element present in the ArrayList at the index.
The syntax to get element from ArrayList is
</>
Copy
E element = arraylist_1.get(index);
get() method fetches and returns the element present in the ArrayList arraylist_1
at specified index
.
Example 1 – Get Element from ArrayList
In the following example, we initialize an ArrayList and get the element at index 2.
Java Program
</>
Copy
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> arraylist_1 = new ArrayList<String>(
Arrays.asList("apple", "banana", "mango", "orange"));
String element = arraylist_1.get(2);
System.out.println(element);
}
}
Output
mango
Conclusion
In this Java Tutorial, we learned how to get element from ArrayList at a specific index.