Skip to content

Commit bad4042

Browse files
committed
Add linkedlist set method example
1 parent 49c4ee2 commit bad4042

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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("\nLinkedList after setting an element BR at index 4:");
31+
System.out.println(linkedList);
32+
33+
System.out.println("\nPreviously present element at index 4:");
34+
System.out.println(previousElement);
35+
}
36+
}

0 commit comments

Comments
 (0)