File tree Expand file tree Collapse file tree 3 files changed +90
-0
lines changed
collections/linkedlist/get-an-element-from-linkedlist Expand file tree Collapse file tree 3 files changed +90
-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 an element from
7
+ * the linkedlist by it's index.
8
+ *
9
+ * @author coderolls.com
10
+ */
11
+ public class LinkedListGetElement {
12
+
13
+ public static void main (String [] args ) {
14
+
15
+ LinkedList <String > linkedList = new LinkedList <String >();
16
+ linkedList .add ("Adidas" );
17
+ linkedList .add ("Air" );
18
+ linkedList .add ("Nike" );
19
+ linkedList .add ("Puma" );
20
+ linkedList .add ("Reebok" );
21
+
22
+ System .out .println ("LinkedList: " );
23
+ System .out .println (linkedList );
24
+
25
+ // get element at index 3
26
+ String element = linkedList .get (3 ); // Puma
27
+
28
+ System .out .println ("\n Element at index 3 is: " + element );
29
+ }
30
+ }
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 element from
7
+ * the linkedlist using getFirst() method.
8
+ *
9
+ * @author coderolls.com
10
+ */
11
+ public class LinkedListGetFirst {
12
+
13
+ public static void main (String [] args ) {
14
+
15
+ LinkedList <String > linkedList = new LinkedList <String >();
16
+ linkedList .add ("Adidas" );
17
+ linkedList .add ("Air" );
18
+ linkedList .add ("Nike" );
19
+ linkedList .add ("Puma" );
20
+ linkedList .add ("Reebok" );
21
+
22
+ System .out .println ("LinkedList: " );
23
+ System .out .println (linkedList );
24
+
25
+ // get first element
26
+ String element = linkedList .getFirst (); // Adidas
27
+
28
+ System .out .println ("\n First element of the linkedList is: " + element );
29
+ }
30
+ }
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 element of
7
+ * the linkedlist using getLast() method.
8
+ *
9
+ * @author coderolls.com
10
+ */
11
+ public class LinkedListGetLast {
12
+
13
+ public static void main (String [] args ) {
14
+
15
+ LinkedList <String > linkedList = new LinkedList <String >();
16
+ linkedList .add ("Adidas" );
17
+ linkedList .add ("Air" );
18
+ linkedList .add ("Nike" );
19
+ linkedList .add ("Puma" );
20
+ linkedList .add ("Reebok" );
21
+
22
+ System .out .println ("LinkedList: " );
23
+ System .out .println (linkedList );
24
+
25
+ // get first element
26
+ String element = linkedList .getLast (); // Adidas
27
+
28
+ System .out .println ("\n Last element of the linkedList is: " + element );
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments