Java Arraylist Indexof
## Java ArrayList indexOf() Method
The `indexOf()` method of the `ArrayList` class in Java is used to find the index of the first occurrence of a specified element in a dynamic array.
---
## Syntax
The syntax for the `indexOf()` method is as follows:
```java
public int indexOf(Object obj)
```
### Parameters
* **`obj`**: The element to search for in the `ArrayList`.
### Return Value
* Returns the **index** (0-based) of the first occurrence of the specified element in the list.
* If the element is not found in the `ArrayList`, the method returns **`-1`**.
---
## Code Examples
### Example 1: Finding the Index of an Element
The following example demonstrates how to find the index of an existing element and an element that does not exist in the `ArrayList`.
```java
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList sites = new ArrayList<>();
// Add elements to the ArrayList
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
System.out.println("Website List: " + sites);
// Find the index of the element "Runoob"
int position1 = sites.indexOf("Runoob");
System.out.println("Index of 'Runoob': " + position1);
// Find the index of the element "Weibo" (which does not exist)
int position2 = sites.indexOf("Weibo");
System.out.println("Index of 'Weibo': " + position2);
}
}
```
**Output:**
```text
Website List: [Google, Runoob, Taobao]
Index of 'Runoob': 1
Index of 'Weibo': -1
```
**Key Takeaways from Example 1:**
* `sites.indexOf("Runoob")` returns `1` because "Runoob" is at index 1.
* `sites.indexOf("Weibo")` returns `-1` because "Weibo" is not present in the list.
---
### Example 2: Handling Duplicate Elements
If an element appears multiple times in the `ArrayList`, `indexOf()` will only return the index of its **first** occurrence.
```java
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList sites = new ArrayList<>();
// Add elements, including duplicates
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Runoob"); // Duplicate element
System.out.println("Website List: " + sites);
// Find the index of the element "Runoob"
int position1 = sites.indexOf("Runoob");
System.out.println("Index of 'Runoob': " + position1);
}
}
```
**Output:**
```text
Website List: [Google, Runoob, Taobao, Runoob]
Index of 'Runoob': 1
```
**Key Takeaways from Example 2:**
* Even though "Runoob" appears at both index `1` and index `3`, the `indexOf()` method returns `1` because it stops searching after finding the first match.
---
## Important Considerations
### 1. How Equality is Determined
The `indexOf()` method determines equality by traversing the list and comparing the target object using the `equals()` method:
* If the target object `obj` is `null`, it searches for the first element in the list that is also `null`.
* If `obj` is not `null`, it searches for the first element `e` such that `obj.equals(e)` returns `true`.
### 2. Finding the Last Occurrence
If you need to find the index of the **last** occurrence of a duplicate element in an `ArrayList`, use the `lastIndexOf()` method instead.
### 3. Retrieving Elements by Index
If you already know the index and want to retrieve the element stored at that position, use the `get(int index)` method.
YouTip