Presentation is loading. Please wait.

Presentation is loading. Please wait.

List Interface and Linked List Mrs. Furman March 25, 2010.

Similar presentations


Presentation on theme: "List Interface and Linked List Mrs. Furman March 25, 2010."— Presentation transcript:

1 List Interface and Linked List Mrs. Furman March 25, 2010

2 List Interface  A class that implements List is a sequence of elements.  Duplicates are allowed.  Indexes start at 0 (first index) … length -1  A list allows you to:  Access an element at any position in the list using its integer index.  Insert an element anywhere in the list  Iterate over all elements using ListIterator or Iterator.

3 List Interface Methods of List: boolean add(Object obj) – adds element at end void add (int index, Object obj) – inserts at position int size() – returns number of elements Object get (int index) – returns the Object at the index Object set(int index, Object obj) – sets the element at the index to obj. Iterator iterator() ListIterator <> listIterator()

4 Some reminders… ArrayList is an array implementation of List interface. ArrayLists can change size at run-time, while an array has a fixed size. Shifting elements caused by insertion and deletion is handled automatically with ArrayLists.

5 ArrayList Methods for ArrayList: same as methods for List, since List is an interface, we have to implement each one of their methods.

6 Side Notes on ArrayList add, get, remove and set all take indexes, that can be out of bounds, and therefore throw exceptions. get, remove, and set are out of bounds if.. index = size() index = size() add can use the index that is equal to size… so index size() is out of bounds.

7 Why have a List interface?  We use interfaces when we have multiple abstract commonalities between classes.  There are multiple ways we can represent a List  There are other types of lists that implement the List interface.

8 Linked Lists  Differ from Arraylist because storage isn’t necessarily all together in memory.  Each element (node) stores two things:  it value  a reference to where the next element is located  The last element stores a null pointer as the location of the next element.  Dynamic Storage: grows and shrinks during run- time! When an element is no longer being referred to  Java has automatic garbage collection.

9 Several Types of Linked Lists  Linear – links in one direction. From the first node  the last node.  Doubly Linked – link in both directions, so that you can go forward and backward through the list  Circular Linked – The last node refers back to the first.

10 LinkedList methods  LinkedList() – constructor  void addFirst(Object obj) – inserts obj at the front of the list.  void addLast (Object obj) – appends obj to the end of the list.  Object getFirst() – returns first element  Object getLast() – returns the last element  Object removeFirst() – Removes and returns the first element in the list  Object removeLast() – Removes and returns the last element in the list.

11 ArrayList vs. LinkedList Insert at front add(0, obj) O(n) shifts all the element addFirst (obj) O(1). Insert at End add(obj)O(1) O(n) – adding n elements add(obj)O(1) Delete at front remove(0) O(n) – shifting n elements down removeFirst()O(1) Delete at End remove (size() -1 ) O(1)removeLast()O(1) Insert in middle add(Index, obj) O(1) – find the spot O(n) – insert the element. itr.add() O(n) – find the spot O(1) – insert.

12 Delete in middle remove (index) O(1) – access O(n) – shift the elements into the void itr.remove() O(n) – access to insertion point O(1) – delete Change value in middle set (index, obj) O(1). Fast access set (index, obj) O(n) traversal to location element For most applications ArrayList is faster! ArrayList has fast access to any element, while LinkedList has to traverse the list to get to anything but 1 st and last. LinkedList needs to allocate a node for each element in the list whereas ArrayList does not. Use LinkedLists for: Programs that require frequent additions to front of list. If you need to iterate through the entire list deleting elements as you go

13 Exercises Which of the following will compile without error? 1. List list = new List(); 2. List list = new ArrayList(); 3. List list = new LinkedList(); 4. ArrayList list = new List(); 5. ArrayList list = new ArrayList(); 6. LinkedList list = new List(); 7. LinkedList list = new ArrayList();

14 Using an Iterator  It is often helpful to use a ListIterator rather than a loop

15 Iterators ListIterator interface methods: next – gets the next element of the collection hasNext – returns if there is a next element remove – removes the current element from the collection.

16 Iterators Cont.  Each collection class defines it own implementation of the Iterator methods in a class that’s invisible to the user of the collection  An Iterator object is created using the iterator method.  Iterator itr = c.iterator();  This point itr at the start of the collection. All elements are returned in proper order.

17 Iterators Cont.  Traversal through a list with an iterator: for (itr = c.iterator(); itr.hasNext();) { /*code with a call to itr.next() which advances itr, so we do not need a step expression in our for loop*/ } List, ArrayList and LinkedList have a expanded Iterator… ListIterator, which allows us to move in both directions, as well as add() and set()

18 Divide into 2 groups  You will each be given the first node of a list. Please record the value of this node, and iterate through the list, finding each of the other nodes in the list. When you reach the null pointer, you can return to the room, and output your list to the board. Have Fun!!


Download ppt "List Interface and Linked List Mrs. Furman March 25, 2010."

Similar presentations


Ads by Google