How To Get An Index of the Element Of LinkedList?
In this tutorial, we will see how to get an index of the element from the LinkedList. LinkedList class provides various methods to get an index of the element of the LinkedList. Let’s see this method with example.
Introduction
LinkedList class is present in the java.util
package.
LinkedList internally uses a doubly linkedlist for storing the variables. That’s why LinkedList is preferred when our frequent operation is insertion and deletion.
LinkedList class provides following method to an element from the list.
indexOf(Object o)
lastIndexOf(Object o)
1. indexOf(Object o)
The indexOf(Object o)
method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
The method signature is asgiven below.
1
public int indexOf(Object o)
This method uses equals()
method to check the equality of the element from linkedList and specifed element as a parameter.
I have given a java program to get an index of the element of the LinkedList below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.LinkedList;
/**
* A Java program to get the first index of the element from
* the linkedlist using indexOf(Object o) method.
*
* @author coderolls.com
*/
public class LinkedListIndexOf {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("PepsiCo");
linkedList.add("DrPepper");
linkedList.add("GoldSpot");
linkedList.add("GoldSpot");
linkedList.add("CocaCola");
linkedList.add("Moxie");
linkedList.add("GoldSpot");
linkedList.add("Moxie");
System.out.println("LinkedList: ");
System.out.println(linkedList);
// get first index of GoldSpot
int index = linkedList.indexOf("DrPepper"); // 1
System.out.println("\nIndex of the DrPepper in linkedList is: "+ index);
// get first index of GoldSpot
int indexGS = linkedList.indexOf("GoldSpot"); // 2
System.out.println("\nIndex of the GoldSpot in linkedList is: "+ indexGS);
// get first index of Moxie
int indexMoxie = linkedList.indexOf("Moxie"); // 5
System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie);
}
}
Output:
1
2
3
4
5
6
7
8
LinkedList:
[PepsiCo, DrPepper, GoldSpot, GoldSpot, CocaCola, Moxie, GoldSpot, Moxie]
Index of the DrPepper in linkedList is: 1
Index of the GoldSpot in linkedList is: 2
Index of the Moxie in linkedList is: 5
2. lastIndexOf(Object o)
The lastIndexOf(Object o)
method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
The method signature for the lastIndexOf(Object o)
method is given below.
1
public int lastIndexOf(Object o)
This method uses equals()
method to check the equality of the element from linkedList and specifed element as a parameter.
I have given a java program to get the last index of the element of the LinkedList below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.LinkedList;
/**
* A Java program to get the last index of the element from
* the linkedlist using lastIndexOf(Object o) method.
*
* @author coderolls.com
*/
public class LinkedListLastIndexOf {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("PepsiCo");
linkedList.add("DrPepper");
linkedList.add("GoldSpot");
linkedList.add("GoldSpot");
linkedList.add("CocaCola");
linkedList.add("Moxie");
linkedList.add("GoldSpot");
linkedList.add("Moxie");
System.out.println("LinkedList: ");
System.out.println(linkedList);
// get last index of GoldSpot
int index = linkedList.lastIndexOf("DrPepper"); // 1
System.out.println("\nLast index of the DrPepper in linkedList is: "+ index);
// get last index of GoldSpot
int indexGS = linkedList.lastIndexOf("GoldSpot"); // 6
System.out.println("\nLast index of the GoldSpot in linkedList is: "+ indexGS);
// get last index of Moxie
int indexMoxie = linkedList.lastIndexOf("Moxie"); // 7
System.out.println("\nLast index of the Moxie in linkedList is: "+ indexMoxie);
}
}
Output:
1
2
3
4
5
6
7
8
LinkedList:
[PepsiCo, DrPepper, GoldSpot, GoldSpot, CocaCola, Moxie, GoldSpot, Moxie]
Last index of the DrPepper in linkedList is: 1
Last index of the GoldSpot in linkedList is: 6
Last index of the Moxie in linkedList is: 7
Conclusion
We can use the and method to get index of the elements from the linkedlist.
-
indexOf(Object o)
- returns the index of the first occurrence of the specified element in this list1 2
// get first index of GoldSpot from the linkedList int index = linkedList.indexOf("GoldSpot");
-
lastIndexOf(Object o)
- returns the index of the last occurrence of the specified element in this list1 2
// get last index of GoldSpot from the linkedList int index = linkedList.lastIndexOf("GoldSpot");
Both the method returns -1, if the specifed element is not present in the LinkedList.
The example java programs given in the above tutorial can be found at this GitHub Repository.
Let me know you thoughts or suggestions on the topic discussed above in comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!