see both Collection interface and List interface for descriptions and examples of LinkedList's methods.
see collections - Finding methods for a chart showing where LinkedList's possible methods are presented in the Java API.
■ LinkedList implements the List Interface and extends the AbstractSequentialList abstract class. It doesn't pick up any new methods from AbstractSequentialList, however, so AbstractSequentialList is bypassed in this book.
■ LinkedList is the modern intended replacement for Stack. It replaces Stack’s push( ) and pop( ) methods with new ones. If you need to explicitly access or manipulate the first or last items in your list then use a LinkedList. LinkedList provides methods for working on the list ends.
■ LinkedList allows duplicates and nulls.
■ Constructors: LinkedList has two constructors,
LinkedList ll = new LinkedList( ); which creates an empty list, and
LinkedList ll = new LinkedList(Collection c); which turns any Collection into a LinkedList. i.e.
LinkedList ll = new LinkedList( ); creates a new empty LinkedList
Vector v = new Vector( );
v.add( "my" );
v.add( "new" );
v.add(
"LinkedList" );
LinkedList ll = new LinkedList( v ); creates a LinkedList containing the three element from v
■ LinkedList adds the stack-like methods below to List: (Note: These are for LinkedList only and don’t work for ArrayList..)
■ This method is like .add(...) except it adds to the bottom of the list, not to the top as .add(...) does. i.e. The snippet adds "Mary" to her appropriate beginning position at the bottom (first position) of the list. For a reminder of where the top and bottom are, see how Lists work.
import java.util.*;
LinkedList ll = new
LinkedList( );
ll.add( "had a"
);
ll.add( "little
lamb" );
ll.addFirst(
"Mary" );
System.out.println("The
list is: " + ll);
■ Ths behaves the same as the regular .add(...) method. It adds to the top (meaning the end) of the list. i.e.
import java.util.*;
LinkedList ll = new
LinkedList( );
ll.add( "Mary
had" );
ll.add( "a
little" );
ll.addLast(
"lamb" );
System.out.println("The
list is: " + ll);
■ This method returns the first or bottom element of
the LinkedList
and is the same as saying ll.get(0) below. The only difference is that .getFirst(
) throws a different exception if the LinkedList has no
elements. See .get(...) under List
interface
i.e. The snippet prints Mary twice.
import java.util.*;
LinkedList ll = new
LinkedList( );
ll.add( "Mary"
);
ll.add( "had"
);
ll.add( "a little
" );
ll.add( "lamb"
);
System.out.println( ll.getFirst(
) );
System.out.println(
ll.get( 0 ) );
■ Returns the last or top element from the LinkedList. That element's index address is ll.size( ) -1. i.e. The snippet prints lamb.
import java.util.*;
LinkedList ll = new
LinkedList( );
ll.add( "Mary"
);
ll.add( "had"
);
ll.add( "a little
" );
ll.add( "lamb"
);
System.out.println(
ll.getLast( ) );
■ This is a dual-action method. It both removes the first or bottom element
from the LinkedList
and also returns it. i.e. The snippet
switches the first element AAA from the bottom to the top of
the list, printing BBB CCC DDD AAA
import java.util.*;
LinkedList ll = new
LinkedList( );
ll.add( "AAA"
);
ll.add( "BBB"
);
ll.add( "CCC"
);
ll.add( "DDD"
);
ll.add( ll.removeFirst( )
);
System.out.println( ll );
■ This is a dual-action method. It removes the list's last or top element
and also returns it. i.e. The snippet
combines it with .addFirst(...) and switches the
top element DDD from the rear to the front of the list,
printing DDD AAA BBB CCC
import java.util.*;
LinkedList ll = new
LinkedList( );
ll.add( "AAA"
);
ll.add( "BBB"
);
ll.add( "CCC"
);
ll.add( "DDD"
);
ll.addFirst(
ll.removeLast( ) );
System.out.println( ll );