Basics on LinkedList

by rajesh 2009-12-30 16:16:42

Basics about java LinkedList


    // Create a new link list
    List list = new LinkedList();
    
    // Add an element to the list
    list.add("1");
    
    // Insert an element at the beginning of the list
    list.add(0, "2");
    
    // Find the total number of elements in the list
    int size = list.size();
    
    // Retrieving the element at the end of the list
    Object element = list.get(list.size()-1);
    
    // Deleting the first occurrence of an element from the link list
    d = list.remove("2");              // returns removed element
d = list.remove("2"); // returns false as there is no such element to remove as its already deleted

    // Delte the element at a particular index
    element = list.remove(0);          

Tagged in:

1178
like
0
dislike
0
mail
flag

You must LOGIN to add comments