1. ArrayList get[] Method
ArrayList.get[int index] method returns the element at the specified position 'index' in the list.
1.1. Syntax
public Object get[ int index ];1.2. Method Parameter
index – index of the element to return. A valid index is always be between 0 [inclusive] to the size of ArrayList [exclusive].
For example, if ArrayList holds 10 objects then a valid argument index will be between 0 to 9 [both inclusive].
1.3. Return Value
The get[] method returns the reference of the object present at the specified index.
1.4. IndexOutOfBoundsException
An invalid index argument will cause IndexOutOfBoundsException error.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.rangeCheck[ArrayList.java:653] at java.util.ArrayList.get[ArrayList.java:429] at com.howtodoinjava.example.ArrayListExample.main[ArrayListExample.java:12]Method indexOf[] Signature
public int indexOf[Object o]
This method returns -1 if the specified element is not present in the list.
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 of the elements to be returned. It is of data-type int.
Return Type:The element at the specified index in the given list.
Exception:It throws IndexOutOfBoundsException if the index is out of range [index=size[]]
Note: Time Complexity: ArrayList is one of the List implementations built a top an array. Hence, get[index] is always a constant time O[1] operation.
Example:
// Java Program to Demonstrate the working of
// get[] method in ArrayList
// Importing ArrayList class
import java.util.ArrayList;
// Main class
public class GFG {
// Main driver method
public static void main[String[] args]
{
// Creating an Empty Integer ArrayList
ArrayList arr = new ArrayList[4];
// Using add[] to initialize values
// [10, 20, 30, 40]
arr.add[10];
arr.add[20];
arr.add[30];
arr.add[40];
// Printing elements of list
System.out.println["List: " + arr];
// Getting element at index 2
int element = arr.get[2];
// Displaying element at specified index
// on console inside list
System.out.println["the element at index 2 is "
+ element];
}
}
|
Example 2: Program to demonstrate the error
// Java Program to Demonstrate Error Generated
// while using get[] method in ArrayList
// Importing ArrayList class
import java.util.ArrayList;
// Main class
public class GFG {
// Main driver method
public static void main[String[] args]
{
// Creating an Empty Integer ArrayList
ArrayList arr = new ArrayList[4];
// Using add[] method to insert elements
// and adding custom values
arr.add[10];
arr.add[20];
arr.add[30];
arr.add[40];
// Getting element at index 2
int element = arr.get[5];
// Print all the elements of ArrayList
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]Java.util.Arraylist.indexOf[] in Java
The indexOf[] method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Syntax :
public int IndexOf[Object o] obj : The element to search for.
// Java code to demonstrate the working of
// indexOf in ArrayList
// for ArrayList functions
import java.util.ArrayList;
public class IndexOfEx {
public static void main[String[] args] {
// creating an Empty Integer ArrayList
ArrayList arr = new ArrayList[5];
// using add[] to initialize values
arr.add[1];
arr.add[2];
arr.add[3];
arr.add[4];
// printing initial value
System.out.print["The initial values in ArrayList are : "];
for [Integer value : arr] {
System.out.print[value];
System.out.print[" "];
}
// using indexOf[] to find index of 3
int pos =arr.indexOf[3];
// prints 2
System.out.println["\nThe element 3 is at index : " + pos];
}
}
|
Output:
The initial values in ArrayList are : 1 2 3 4 The element 3 is at index : 2Practical Application : The index functions are mostly useful to determine last or first occurrence of events, for example last occurrence of 6 in a throw of a die, or 1st occurrence of any letter in a name etc.
One more example:
// Java code to demonstrate the application of
// index functions in ArrayList
// for ArrayList functions
import java.util.ArrayList;
public class AppliIndex {
public static void main[String[] args] {
// creating an Empty Integer ArrayList
ArrayList arr = new ArrayList[10];
// using add[] to initialize dice values
arr.add[1];
arr.add[2];
arr.add[4];
arr.add[6];
arr.add[5];
arr.add[2];
arr.add[6];
arr.add[1];
arr.add[6];
arr.add[4];
// using IndexOf[] to find first index of 6
int pos1 =arr.indexOf[6];
// using lastIndexOf[] to find last index of 6
int pos2 =arr.lastIndexOf[6];
// to balance 0 based indexing
pos1 = pos1+1;
pos2 = pos2+1;
// printing first index of 6
System.out.println["The first occurrence of 6 is : " + pos1];
// printing last index of 6
System.out.println["The last occurrence of 6 is : " + pos2];
}
}
|
Output:
The first occurrence of 6 is : 4 The last occurrence of 6 is : 9This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.