How to use the LinkedList class

Description:

  • A LinkedList is a collection that's similar to an array list. However, the LinkedList class doesn't use an array to store its elements. Instead, each element in the list contains pointers that are used to refer to adjecent elements.
  • You can specify the type of elements the LinedList can contain by listing the type in the angle brackets.
  • The LinkedList class contains methods that let you perform more advanced operations that the ArrayList class.

Constructor Description
LinkedList<E>() Creates an empty linked list using the specific type.

Method Description
add(object) * Adds to specified object to the end of the list.
add(index,object) * Adds the specified object at the specified index position
addFirst(object) Add the specified object at the beginning of the list.
addLast(object) Add the specified object at the end of the list.
clear() * Removes all elements from the list.
contains(object) * Returns true if the spedified object is in the list.
get(index) * Return the object at the specified index position.
getFirst() Returns the first element in the list.
getLast() Returns the last element in the list.
indexOf(object) * Return the index position of the specified object.
peek() Returns but doesn't remove the first element in the list.
offer(object) Attempts to add the speciied object to the end of the list. Returns true if the object was added. Returns false if the object is rejected.
poll() Returns and removes the first element from the list. Returns null if the list is empty.
remove() Returs and removes the first element from the list. Throws NoSuchElementExcetion if the list is empty.
remove(index) * Removes the object at the specified index.
remove(object) * Removes the specified object.
removeFirst() Removes and returns the first element of the list.
removeLast() Removes and returns the last element of the list.
set(index,object) * Sets the element at the specified index to the specified object
size() * Returs the number of elements in the list.
toArray() * Returns an array containing the elements of the list.

* = same as ArrayList class