The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util  [50 examples] > Lists  [3 examples]

e349. Creating a List

    // Create the list
    List list = new LinkedList();    // Doubly-linked list
    list = new ArrayList();          // List implemented as growable array
    
    // Append an element to the list
    list.add("a");
    
    // Insert an element at the head of the list
    list.add(0, "b");
    
    // Get the number of elements in the list
    int size = list.size();          // 2
    
    // Retrieving the element at the end of the list
    Object element = list.get(list.size()-1);   // a
    
    // Retrieving the element at the head of the list
    element = list.get(0);                      // b
    
    // Remove the first occurrence of an element
    boolean b = list.remove("b");      // true
    b = list.remove("b");              // false
    
    // Remove the element at a particular index
    element = list.remove(0);          // a

 Related Examples
e350. Sorting a List
e351. Operating on Lists

See also: Arrays    Bits    Collections    Dates    Hash Tables    Property Files    Sets    Sorted Collections    Time    Timers   


© 2002 Addison-Wesley.