Example usage for java.util LinkedList LinkedList

List of usage examples for java.util LinkedList LinkedList

Introduction

In this page you can find the example usage for java.util LinkedList LinkedList.

Prototype

public LinkedList() 

Source Link

Document

Constructs an empty list.

Usage

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // retrieve and remove the head of the list
    System.out.println("Head element of the list:" + list.poll());

    // print the list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // retrieve and remove the first element of the list
    System.out.println("First element of the list:" + list.pollFirst());

    // print the list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // add a new element at the beggining of the list
    list.addFirst("Element");

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // retrieve and remove the last element of the list
    System.out.println("Last element of the list:" + list.pollLast());

    // print the list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // check if the list contains "Coffee"
    System.out.println("List contains 'Coffee':" + list.contains("Coffee"));

    // check if the list contains "10"
    System.out.println("List contains '10':" + list.contains("10"));
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // add a new element at the end of the list
    list.add(2, "Element");

    // print the updated list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // add a new element at the end of the list
    list.add("Element");

    // print the updated list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // pop the list
    System.out.println("Pop element in the list:" + list.pop());

    // print the list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // get the last index for "Hello"
    System.out.println("Index for Hello:" + list.lastIndexOf("Hello"));

    // get the index for "Coffee"
    System.out.println("Index for Coffee:" + list.indexOf("Coffee"));
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // clear the list
    list.clear();// www. jav  a 2  s  .co m

    // print the list
    System.out.println("LinkedList:" + list);
}