Introduction
The indexOf()
method in Java's LinkedList
class is a handy tool for finding the first occurrence of a specified element within the list. Think of it like searching for a specific book on a bookshelf – you want to know its position if it exists.
Syntax
public int indexOf(Object o)
Parameters
Parameter | Description |
---|---|
o |
The element to search for within the list. This can be any object. If the list doesn't contain this object, it returns -1. |
Return Value
The indexOf()
method returns the index of the first occurrence of the specified element in the list. If the element is not found, it returns -1.
Examples
Example 1: Finding an Existing Element
This example demonstrates how to use indexOf()
to find a value that's already present in the LinkedList.
import java.util.LinkedList;
public class LinkedListIndexOfExample1 {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("apple");
myList.add("banana");
myList.add("cherry");
int index = myList.indexOf("banana");
System.out.println(index);
}
}
1
Explanation: We create a LinkedList of Strings and add three elements. Then, we call indexOf()
to find the index of "banana". Since "banana" is at index 1, that's what gets returned.
Example 2: Finding an Element That Doesn’t Exist
This example shows what happens when you try to find an element that isn't in the list. It demonstrates how `indexOf()` returns -1 in such cases.
import java.util.LinkedList;
public class LinkedListIndexOfExample2 {
public static void main(String[] args) {
LinkedList<Integer> myList = new LinkedList<>();
myList.add(10);
myList.add(20);
myList.add(30);
int index = myList.indexOf(40); // 40 is not in the list
System.out.println(index);
}
}
-1
Explanation: We create a LinkedList of Integers and try to find 40, which isn't present in the list. Therefore, indexOf()
returns -1.
Example 3: Using indexOf() with Objects
This example illustrates using indexOf()
with custom objects. The equals method of the object is used to compare for equality.
import java.util.LinkedList;
class MyObject {
int value;
public MyObject(int value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MyObject) {
return this.value == ((MyObject) obj).value;
} else {
return false;
}
}
}
public class LinkedListIndexOfExample3 {
public static void main(String[] args) {
LinkedList<MyObject> myList = new LinkedList<>();
myList.add(new MyObject(5));
myList.add(new MyObject(10));
myList.add(new MyObject(15));
MyObject target = new MyObject(10);
int index = myList.indexOf(target);
System.out.println(index);
}
}
1
Explanation: We create a LinkedList of MyObject
instances and then use `indexOf()` to find an object with the value 10. The equals method in MyObject is used for comparison, so it correctly identifies the element at index 1.
Example 4: Handling Null Values
This example demonstrates what happens when you attempt to search for a null value within a LinkedList.
import java.util.LinkedList;
public class LinkedListIndexOfExample4 {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("apple");
myList.add(null);
myList.add("cherry");
int index = myList.indexOf(null);
System.out.println(index);
}
}
1
Explanation: The LinkedList contains a null element at index 1, so indexOf(null) returns 1.