Collections and Generics
LinkedList class
Class
Since MyArrayList is implemented using an array, the methods get(index) and set(index, object) for accessing and modifying an element through an index and the add(object) for adding an element at the end of the list are efficient. However, the methods add(index, object) and remove(index) are inefficient because it requires shifting potentially a large number of elements. You can use a linked structure to implement a list to improve efficiency for adding and remove an element anywhere in a list.
An introduction to linked lists
- A linked list is a collection that's similar to an array list, but it doesn't use an array to store its elements. Instead, each element contains pointers that are used to refer to adjacent elements.
- You can specify the type of elements the linked list can contain by listing the type in angle brackets.
- The LinkedList class contains methods that let you perform more advanced operations than the ArrayList class.
The LinkedList class
java.util.LinkedList
A constructor for the LinkedList class
Constructor | Description |
---|---|
LinkedList | Creates an empty linked list using the specified type. |
Nodes in Linked Lists
A linked list consists of nodes. Each node contains an element, and each node is linked to its next neighbor. Thus a node can be defined as a class, as follows:
class Node { Object element; Node next; public Node(Object o) { element = o; } }
The variable first refers to the first node in the list, and the variable last refers to the last node in the list. If the list is empty, both are null. For example, you can create three nodes to store three circle objects (radius 1, 2, and 3) in a list:
Node first, last; // Create a node to store the first circle object first = new Node(new Circle(1)); last = first; // Create a node to store the second circle object last.next = new Node(new Circle(2)); last = last.next; // Create a node to store the third circle object last.next = new Node(new Circle(3)); last = last.next;
Common methods of the LinkedList class
Method | Description |
---|---|
add(object) | Adds the specified object to the list. |
add(index, object) | Adds the specified object at the specified index position. |
addFirst(object) | Adds the specified object to the beginning of the list. |
addLast(object) | Adds the specified object to the end of the list. |
clear() | Removes all elements from the list. |
contains(object) | Returns true if the specified object is in the list. |
get(index) | Returns 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) | Returns 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 specified 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() | Returns and removes the first element from the list. Throws NoSuchElementException if the list is empty. |
remove(index) | Removes and returns the object at the specified index position. |
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) | Replaces the element at the specified index position with the specified object. |
size() | Returns the number of elements in the list. |
toArray() | Returns an array containing the elements of the list. |
The addFirst(object) Method
The addFirst(object) method creates a new node to store the element and insert the node to the beginning of the list. After the insertion, first should refer to this new element node.
The addLast(object) Method
The addLast(object) method creates a node to hold element o and insert the node to the end of the list. After the insertion, last should refer to this new element node.
The add(index, object) Method
The add(index, object) method adds an element object to the list at the specified index. Consider three cases:
- if index is 0, invoke addFirst(object) to insert the element to the beginning of the list;
- if index is greater than or equal to size, invoke addLast(object) to insert the element to the end of the list;
- create a new node to store the new element and locate where to insert the new element. The new node is to be inserted between the nodes current and temp. The method assigns the new node to current.next and assigns temp to the new node's next.
The removeFirst() Method
The removeFirst() method removes the first node in the list by pointing first to the second node. The removeLast() method removes the last node from the list. Afterwards, last should refer to the former second-last node.