The Java library jas
a LinkedList class
that is
iterable:
Recall:
be iterable means you can
use the
object in the
for-each-loop
Implementation details of the
LinkedList<E> class
Java'slinkedList<E> class
is implemented as
a doubly-linked list:
Sample methods:
size():
Returns the number of elements in this list
addFirst(E e)
addLast(E e)
add(int index, E element)
removeFirst()
removeLast()
remove(int index)
getFirst()
getLast()
get(int index)
How to use
Java's LinkedList
class
The syntax is the
same as
our own
GenericLinkedList<sT>class
Here is a program that make use
of Java'sLinkedList class
import java.util.LinkedList;
public class Demo
{
public static void main(String[] args)
{
LinkedList<String> stringList = new LinkedList<String>();
stringList.addFirst("B"); // B
stringList.addFirst("A"); // A -> B
stringList.addLast("D"); // A -> B -> D
stringList.add(2, "C"); // A -> B -> C -> D
System.out.println("LIST = " + stringList);
String s;
s = stringList.remove(2); // A -> B -> D
s = stringList.removeLast(); // A -> B
s = stringList.removeFirst(); // B
}
}