ArrayList get(index) method in Java with examples
The get() method of ArrayList in Java is used to get the element of a specified index within the list.
Syntax :
get(index)
Parameter :
index:index of the elements to be returned. It is of data-type int.
Returns :
It returns the element at the specified index in the given list.
Errors and exception :
IndexOutOfBoundsException-if the index is out of range (index=size())
Example 1 : Program to demonstrate the working of get()
// Java code to demonstrate the working of // get() method in ArrayList // for ArrayList functions import java.util.ArrayList; public class GFG { public static void main(String[] args) { // creating an Empty Integer ArrayList ArrayList<Integer> arr = new ArrayList<Integer>(4); // using add() to initialize values // [10, 20, 30, 40] arr.add(10); arr.add(20); arr.add(30); arr.add(40); System.out.println("List: " + arr); // element at index 2 int element = arr.get(2); System.out.println("the element at index 2 is " + element); } } |
Output :
List: [10, 20, 30, 40] the element at index 2 is 30
Example 2 : Program to demonstrate the error
// Java code to demonstrate the error of // get() method in ArrayList // for ArrayList functions import java.util.ArrayList; public class GFG { public static void main(String[] args) { // creating an Empty Integer ArrayList ArrayList<Integer> arr = new ArrayList<Integer>(4); // using add() to initialize values // [10, 20, 30, 40] arr.add(10); arr.add(20); arr.add(30); arr.add(40); // element at index 2 int element = arr.get(5); System.out.println("the element at index 2 is " + element); } } |
Output :
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at GFG.main(GFG.java:22)
Reference: link
Recommended Posts:
- CharacterIterator getIndex() method in Java with Examples
- StringCharacterIterator getIndex() method in Java with Examples
- ParsePosition getIndex() method in Java with Example
- ArrayList set() method in Java with Examples
- ArrayList subList() method in Java with Examples
- ArrayList clone() method in Java with Examples
- ArrayList ensureCapacity() method in Java with Examples
- ArrayList iterator() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- ArrayList toArray() method in Java with Examples
- ArrayList listIterator() method in Java with Examples
- ArrayList size() method in Java with Examples
- Arraylist removeRange() in Java with examples
- ArrayList clear() in Java with examples
- ArrayList and LinkedList remove() methods in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


