File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
collections/linkedlist/set-element-at-an-index-in-linkedlist Expand file tree Collapse file tree 1 file changed +36
-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 set an element at specified index
7+ * using the set(int index, E element) method.
8+ *
9+ * @author coderolls.com
10+ */
11+ public class LinkedListSet {
12+
13+ public static void main (String [] args ) {
14+ LinkedList <String > linkedList = new LinkedList <String >();
15+
16+ linkedList .add ("IN" );
17+ linkedList .add ("US" );
18+ linkedList .add ("FR" );
19+ linkedList .add ("JP" );
20+ linkedList .add ("CN" );
21+ linkedList .add ("RU" );
22+
23+ System .out .println ("LinkedList before setting an element at index 4:" );
24+ System .out .println (linkedList );
25+
26+ // replaces CN with BR
27+ // returns the previously present element at index 4 i.e CN
28+ String previousElement = linkedList .set (4 , "BR" );
29+
30+ System .out .println ("\n LinkedList after setting an element BR at index 4:" );
31+ System .out .println (linkedList );
32+
33+ System .out .println ("\n Previously present element at index 4:" );
34+ System .out .println (previousElement );
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments