/**
* @author lautturi.com
* Java example: Java LinkedList examples
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
//Create a linked list object
LinkedList<String> list = new LinkedList<String>();
// add new elements to linked list in java
list.add("java");
list.add("python");
list.add("js");
list.add("php");
list.add("hello");
// add new item to the end of the linked list in java
list.addLast("lautturi");
// add new item to the first position in the linked list in java
list.addFirst("perl");
// Insert item into list at the location with index 2
list.add(2,"c");
// print the list
System.out.println(list);
// remove the element from linked list by value
list.remove("hello");
// remove the element from linked list by index
list.remove(3);
// print the list after deleting elements
System.out.println(list);
// delete the first element from the linked list in java
list.removeFirst();
// delete the last element from the linked list in java
list.removeLast();
// get the element at the position 4+1(index 4)
String str = list.get(3);
// modify item of linked list in java
list.set(3, "c sharp");
// print the list
System.out.println(list);
}
}
output:
[perl, java, c, python, js, php, hello, lautturi] [perl, java, c, js, php, lautturi] [java, c, js, c sharp]