Return | Method | Summary |
---|---|---|
boolean | add(E e) | Appends the specified element to the end of this list. |
void | add(int index, E element) | Inserts the specified element at the specified position in this list. |
boolean | addAll(Collection<? extends E> c) | Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
boolean | addAll(int index, Collection<? extends E> c) | Inserts all of the elements in the specified collection into this list, starting at the specified position. |
void | addFirst(E e) | Inserts the specified element at the beginning of this list. |
void | addLast(E e) | Appends the specified element to the end of this list. |
boolean | offer(E e) | Adds the specified element as the tail (last element) of this list. |
boolean | offerFirst(E e) | Inserts the specified element at the front of this list. |
boolean | offerLast(E e) | Inserts the specified element at the end of this list. |
import java.util.LinkedList;
public class Main{
public static void main(String args[]) {
LinkedList<String> ll = new LinkedList<String>();
ll.add("A");
ll.add("B");
ll.add("ja v a2s.com");
ll.add("E");
ll.add("F");
ll.add(1, "A2");
System.out.println("Original contents of ll: " + ll);
}
}
The output:
Original contents of ll: [A, A2, B, ja v a2s.com, E, F]
The following code add elements to the start of the LinkedList.
import java.util.LinkedList;
public class Main{
public static void main(String args[]) {
LinkedList<String> ll = new LinkedList<String>();
ll.add("B");
ll.add("ja v a2s.com");
ll.addLast("E");
ll.add("F");
ll.add(1, "A2");
ll.addFirst("A");
System.out.println("Original contents of ll: " + ll);
}
}
The output:
Original contents of ll: [A, B, A2, ja v a2s.com, E, F]
Storing User-Defined Classes in LinkedList
import java.util.Iterator;
import java.util.LinkedList;
class Address {
private String name;
private String street;
Address(String n, String s) {
name = n;
street = s;
}
public String toString() {
return name + "\n" + street + "\n";
}
}
public class Main{
public static void main(String args[]) {
LinkedList ml = new LinkedList();
ml.add(new Address("Jack", "11 Oak Ave"));
ml.add(new Address("Tom", "1142 Maple St"));
ml.add(new Address("Mary", "867 Elm St"));
Iterator itr = ml.iterator();
while (itr.hasNext()) {
Object element = itr.next();
System.out.println(element + "\n");
}
}
}
The output from the program is shown here:
Jack
11 Oak Ave
Tom
1142 Maple St
Mary
867 Elm St
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |