; 121Nov-08
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

121Nov-08

VIEWS: 2 PAGES: 20

  • pg 1
									                                     15-121
Lists                                   1




   Organizing data
   Lists operations and interface
   Lists implementation
   Iterators
                                                   15-121
Organization of Data                                  2




   Organize data by position
       stacks
       queues
   Organize data by value
       lists (arrays, ArrayLists, linked lists)
       trees
       priority queues
                                             15-121
Lists as Data Structures                        3




   Lists are ordered collections of data
       They can contain duplicate values
       They do have indexes 0 .. length-1
                                     15-121
List Operations                         4




   initialize the list
   is the list empty?
   is the list full?
   number of elements in the list
   inserting an item
   deleting an item
   retrieving an item
                                                      15-121
List Implementation                                      5




   Array-based (arrays)
       Uses ArrayList (or our own implementation)
       pros and cons


   Reference-based (linked list)
       Uses LinkedList (or our own implementation)
       pros and cons
                                          15-121
List interface in Java                       6




import java.util.*;

   Java has am interface called List
   look it up at the API Java web site
                                                             15-121
Iterators                                                       7




   An object that is used to traverse a collection of
    values (lists, sets, and maps).

   It is liked a generalized reference that starts at the
    beginning of a collection (list) and can move along
    the collection (list) from one item to the next.

   Iterators are defined by a parameterized interface
    named Iterator<T>
                                                                15-121
Iterator interface                                                 8




   E next()
       returns the next item or an exception
       advances the iterator

   boolean hasNext()
       returns true if there is at least one more item to be
        processed

   void remove()
       Removes the most recent item returned by next() or an
        exception
                                                         15-121
Using an iterator                                           9




   Assuming we have a list of Integers (List<Integer>
    alist)

    Iterator<Integer> iter = alist.iterator();
    while (iter.hasNext()) {
       int num = iter.next();
       if (num % 2 == 0)
                iter.remove();
    }
                                               15-121
Why do we use Iterators?                         10




public static <E> void print(List<E> list) {
  for (int i = 0; i < list.size(); i++) {
       System.out.print(list.get(i) + " ");
  }
}
Runtime for list.get(i)
      ArrayList is O(1)
      LinkedList is O(n)
Overall runtime
      ArrayList is O(n)
      LinkedList is O(n2)
                                                15-121
How do we use Iterators?                          11




public static void print(List<String> list) {
    Iterator<String> it = list.iterator();
    while(it.hasNext()) {
       System.out.print(it.next());
    }
}
   Runtime is always O(n). How?
                                                 15-121
Things to watch for … (1)                          12




public static void print(List<String> list) {
    Iterator<String> it = list.iterator();
    String x = it.next();
    while(x != null) {
       System.out.print(it.next());
       x = it.next();
    }
}

Problem: it.next() never returns null. Lesson?
                                                      15-121
Things to watch for … (2)                               13




public static void print(List<String> list) {
  while(list.iterator().hasNext())
      System.out.print(list.iterator().next());
}

Problem: infinite loop. How? Prints the first value
multiple times. Lesson?
                                                15-121
Things to watch for … (3)                         14




public static void print(List<String> list) {
    Iterator<String> it = list.iterator();
    while(it.hasNext()) {
       if(it.next().equals(“Hi”);
               System.out.print(it.next());
    }
}

Problem: always print the same value. Lesson?
                                                           15-121
Iterator method                                              15




 Every collection in Java has an iterator() method
            Iterator<E> iterator();
This method returns an iterator object for the
collection.

   Collections are:
       Lists (arrays in general, ArrayList, LinkedList)
       Sets
       Maps
                                                    15-121
Implementing an Iterator class                        16



private class MyIterator implements Iterator<E> {
   public Node<E> node;

     public MyIterator () {
        node = front;
    }

     public boolean hasNext() {
        return node != null;
    }
}
                                                           15-121
Implementing a remove()                                      17




public static void remove(List<String> list, String s) {
  Iterator<String> it = list.iterator();
  while(it.hasNext()) {
     if(it.next().equals(s);
             it.remove();
  }
}
Overall runtime
      ArrayList is O(n)
      LinkedList is O(1)
                                                           15-121
Things to watch for … (4)                                    18



public static void remove(List<String> list, String s) {
  Iterator<String> it = list.iterator();
  int index = 0;
  while(it.hasNext()) {
        if(it.next().equals(s);
                 list.remove(index);
        else
                 index++;
  }
}

Problem: crashes. Why? Lesson?
                                                           15-121
Enhanced For loop                                            19




   Iterators allows Java to have a condensed version of
    the for loop
        for (parameter : collection) { …}


   Assuming we have a collection of values

    for (n : alist) {
      if (n % 2 == 0)
                 alist.remove(n);
    }
                                                   15-121
Homework                                             20




   No quiz tomorrow 11/09

   Exam II on Thursday 11/10

   Homework #8 (binary trees) due Tuesday 11/15

								
To top