Java Tutorial
LinkedList()This constructor builds an empty linked list.
LinkedList(Collection collection)This constructor builds an linked list that is initialized with the elements of the collection.
void addFirst(Object)
void addLast(Object)
Object getFirst()
Object getLast()
Object removeFirst()
Object removeLast()
boolean add(Object)adds new object at end of the array list.
boolean add(int, Object)adds new object at the specified index of array list.
Object get(int index)
void set(int index, Object obj)
// Codingpointer Demonstrates LinkedList import java.util.*; class LinkedListTest { public static void main(String args[]) { // Creates LinkedList LinkedList ll = new LinkedList(); // add linked list elements. ll.add(new Integer(5)); ll.add(new Integer(1)); ll.add(new Integer(3)); ll.add(new Integer(2)); ll.add(new Integer(4)); ll.addFirst("start"); ll.addLast("end"); ll.add(2, "insert"); System.out.println("Size of Linked list: "+ ll.size()); // prints linked list elements System.out.println("Elements in linked list: " + ll); } }Output:
$ java LinkedListTest Size of Linked list: 8 Elements in linked list: [start, 5, insert, 1, 3, 2, 4, end]
// Codingpointer Demonstrates LinkedList import java.util.*; class LinkedListTest { public static void main(String args[]) { // Creates LinkedList LinkedList ll = new LinkedList(); // add linked list elements. ll.add(new Integer(5)); ll.add(new Integer(1)); ll.add(new Integer(3)); ll.add(new Integer(2)); ll.add(new Integer(4)); ll.addFirst("start"); ll.addLast("end"); ll.add(2, "insert"); // remove elements in linked list ll.remove(2); ll.removeFirst(); ll.removeLast(); System.out.println("Size of Linked list: "+ ll.size()); // prints linked list elements System.out.println("Elements in linked list: " + ll); } }Output:
$ java LinkedListTest Size of Linked list: 5 Elements in linked list: [5, 1, 3, 2, 4]
// Codingpointer Demonstrates LinkedList import java.util.*; class LinkedListTest { public static void main(String args[]) { // Creates LinkedList LinkedList ll = new LinkedList(); // add linked list elements. ll.add(new Integer(5)); ll.add(new Integer(1)); ll.add(new Integer(3)); ll.add(new Integer(2)); ll.add(new Integer(4)); ll.addFirst("start"); ll.addLast("end"); ll.add(2, "insert"); // get and set elements in linked list. Object obj = ll.get(3); System.out.println("get value: " + obj); ll.set(3, (obj.toString() + " changed")); System.out.println("Size of Linked list: "+ ll.size()); // prints linked list elements System.out.println("Elements in linked list: " + ll); } }Output:
$ java LinkedListTest get value: 1 Size of Linked list: 8 Elements in linked list: [start, 5, insert, 1 changed, 3, 2, 4, end]« Previous Next »
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page