Skip to content

Commit 5837e94

Browse files
committed
Add example for get an index of element from linkedlist blog
1 parent 6011fa4 commit 5837e94

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.gaurav.ExProject.LinkedList;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* A Java program to get the first index of the element from
7+
* the linkedlist using indexOf(Object o) method.
8+
*
9+
* @author coderolls.com
10+
*/
11+
public class LinkedListIndexOf {
12+
13+
public static void main(String[] args) {
14+
15+
LinkedList<String> linkedList = new LinkedList<String>();
16+
linkedList.add("PepsiCo");
17+
linkedList.add("DrPepper");
18+
linkedList.add("GoldSpot");
19+
linkedList.add("GoldSpot");
20+
linkedList.add("CocaCola");
21+
linkedList.add("Moxie");
22+
linkedList.add("GoldSpot");
23+
linkedList.add("Moxie");
24+
25+
System.out.println("LinkedList: ");
26+
System.out.println(linkedList);
27+
28+
// get first index of GoldSpot
29+
int index = linkedList.indexOf("GoldSpot"); // 2
30+
31+
System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index);
32+
33+
// get first index of Moxie
34+
int indexMoxie = linkedList.indexOf("Moxie"); // 5
35+
36+
System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.gaurav.ExProject.LinkedList;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* A Java program to get the last index of the element from
7+
* the linkedlist using lastIndexOf(Object o) method.
8+
*
9+
* @author coderolls.com
10+
*/
11+
public class LinkedListLastIndexOf {
12+
13+
public static void main(String[] args) {
14+
15+
LinkedList<String> linkedList = new LinkedList<String>();
16+
linkedList.add("PepsiCo");
17+
linkedList.add("DrPepper");
18+
linkedList.add("GoldSpot");
19+
linkedList.add("GoldSpot");
20+
linkedList.add("CocaCola");
21+
linkedList.add("Moxie");
22+
linkedList.add("GoldSpot");
23+
linkedList.add("Moxie");
24+
25+
System.out.println("LinkedList: ");
26+
System.out.println(linkedList);
27+
28+
// get first index of GoldSpot
29+
int index = linkedList.lastIndexOf("GoldSpot"); // 6
30+
31+
System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index);
32+
33+
// get first index of Moxie
34+
int indexMoxie = linkedList.lastIndexOf("Moxie"); // 7
35+
36+
System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie);
37+
}
38+
}

0 commit comments

Comments
 (0)