9. 12. 5. Adding Elements: add a single element |
|
Adding the element to the end of the list unless an index is specified. |
public boolean add(Object element)
public boolean add(int index, Object element)
|
|
To Treat the linked list as a stack or queue: |
public boolean addFirst(Object element)
public boolean addLast(Object element)
|
|
The addLast() method is equivalent to call add(), with no index. |
import java.util.LinkedList;
public class MainClass {
public static void main(String[] a) {
LinkedList list = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.addFirst("X");
list.addLast("Z");
System.out.println(list);
}
}
|
|
[X, A, B, C, D, Z] |