File tree Expand file tree Collapse file tree 2 files changed +76
-0
lines changed
collections/linkedlist/get-an-index-of-element-of-linkedlist Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
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 ("\n Index 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 ("\n Index of the Moxie in linkedList is: " + indexMoxie );
37
+ }
38
+ }
Original file line number Diff line number Diff line change
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 ("\n Index 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 ("\n Index of the Moxie in linkedList is: " + indexMoxie );
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments