Learn how to get the index of first occurrence of a element in the ArrayList. We will be using ArrayList.indexOf[] method to get the first occurrence.
1. ArrayList.indexOf[] method
This method returns the index of the first occurrence of the specified element in this list. It will return '-1' if the list does not contain the element.
1.1. indexOf[] method syntax
public int indexOf[Object o] { if [o == null] { for [int i = 0; i < size; i++] if [elementData[i]==null] return i; } else { for [int i = 0; i < size; i++] if [o.equals[elementData[i]]] return i; } return -1; }1.2. indexOf[] method parameter
object – the object which needs to be searched in the list for it’s first index position.
1.3. indexOf[] return value
Return value is of int type.
- index – first index position of element if element is found.
- -1 – if element is NOT found.
2. ArrayList get index of element
Java program for how to get first index of object in arraylist. In this example, we are looking for first occurrence of string “brian” in the given list.
We can use this method to find if an object is present in arraylist. If the object is present then return value will be greater than '-1‘.
import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] { ArrayList list = new ArrayList[Arrays.asList["alex", "brian", "charles","alex","dough","gary","alex","harry"]]; int firstIndex = list.indexOf["brian"]; System.out.println[firstIndex]; firstIndex = list.indexOf["hello"]; System.out.println[firstIndex]; } }Note – Please note that arraylist index starts from 0.
Program output.
1 -1Happy Learning !!
Read More:
A Guide to Java ArrayList
ArrayList Java Docs
Let us know if you liked the post. That’s the only way we can improve.
Learn to get an element from an ArrayList using its index position. We will be using ArrayList.get[] method to get the object at the specified index of the arraylist.
ArrayList list = //List instance String firstElement = list.get[0]; String sixthElement = list.get[5];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]2. ArrayList get[] Example
Java program for how to get an object from ArrayList by its index location. In this example, we want to get the object stored at index locations 0 and 1.
import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] { ArrayList list = new ArrayList[Arrays .asList["alex", "brian", "charles", "dough"]]; String firstName = list.get[0]; //alex String secondName = list.get[1]; //brian System.out.println[firstName]; System.out.println[secondName]; } }Program output.
alex brianHappy Learning !!
Let us know if you liked the post. That’s the only way we can improve.
The get[] method of ArrayList in Java is used to get the element of a specified index within the list.
Syntax:
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:
import java.util.ArrayList; public static void main[String[] args] ArrayList arr = new ArrayList[4]; System.out.println["List: " + arr]; int element = arr.get[2]; System.out.println["the element at index 2 is " |
Output List: [10, 20, 30, 40] the element at index 2 is 30
Example 2: Program to demonstrate the error
import java.util.ArrayList; public static void main[String[] args] ArrayList arr = new ArrayList[4]; int element = arr.get[5]; System.out.println["the element at index 2 is " |
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]Practice Tags :
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.
import java.util.ArrayList; public class IndexOfEx { public static void main[String[] args] { ArrayList arr = new ArrayList[5]; arr.add[1]; arr.add[2]; arr.add[3]; arr.add[4]; System.out.print["The initial values in ArrayList are : "]; for [Integer value : arr] { System.out.print[value]; System.out.print[" "]; } int pos =arr.indexOf[3]; 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:
import java.util.ArrayList; public class AppliIndex { public static void main[String[] args] { ArrayList arr = new ArrayList[10]; 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]; int pos1 =arr.indexOf[6]; int pos2 =arr.lastIndexOf[6]; pos1 = pos1+1; pos2 = pos2+1; System.out.println["The first occurrence of 6 is : " + pos1]; 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.
Practice Tags :