CS62 - Spring 2011 - Lecture 9

  • Admin
       - how is assignment 2 going?
       - Prof Bruce will be holding a session tonight in Edmunds 227 at 7:30pm to go over any issues you may have

  • A few last comments on sorting
       - All of the sorting algorithms we've look at are comparison-based sorting algorithms
          - If you can compare two objects, you can sort them
          - There are two ways to tell Java how you want objects to be compared
             - have the object implement the Comparable interface: http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html
                - that is implement compareTo
                   - returns something less than zero if this object is less than the passed in object
                   - returns zero if they're equal
                   - returns something greater than zero if this object is greater than the passed in object
                - should generally also implement equals
             - Implement a comparator: http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html
                - same idea as Comparable, but an external class
       - If you want to sort in Java:
          - Arrays.sort(Object[] a) -- uses merge sort
          - Collections.sort(List list) -- uses quicksort

  • Iterators (java.util.iterator: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Iterator.html)
       - convenient interface for moving through data
       - three methods
          - hasNext(): checks if there are more elements to iterate through
          - next(): give us the next element AND move the iterator forward one element
          - remove(): removes the previously returned method from the last call to next(). NOTE: this is an optional method. Often, when we're looking at these, we won't implement it.
       - look at arraylistIterator method in IteratorExamples class in IteratorExamples code
          - very commonly when we use iterators it's like this

          while( it.hasNext() ){
             // do something with it.next()
          }
       - look at arraylistIteratorConcern in IteratorExamples class in IteratorExamples code
          - notice that each iterator is an independent iterator over the data
          - calling next on one, does not affect the location/order of the other
       - look at arraylistIteratorConcern2 in IteratorExamples class in IteratorExamples code
          - in general, you cannot modify a data structure and then use an iterator from the previously unmodified version
          - why not?
             - behavior isn't defined anymore
       - look at arrayListIteratorConcern3 in IteratorExamples class in IteratorExamples code

  • how does an Iterator know when the data structure it's modifying has been changed?
       - http://java.sun.com/j2se/1.5.0/docs/api/java/util/ConcurrentModificationException.html
       - data structure dependent
          - For any list (ArrayList, Vector, LinkedList, etc), there is a protected field called modCount that is the number of times this list has been structurally modified (http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractList.html)

  • Iterable (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html)
       - just have to implement one method:
          - iterator(), which returns an iterator over this data
       - if a class implements the Iterable interface, then we can use it in the "foreach" loop:
          Iterable<E> myClass;

          for( E s: myClass ){
             // do something with s
          }
       - look at AlphabetSoup class in IteratorExamples code
          - implements Iterable<String>
             - notice that we specify what we're iterating over
          - must include a method:
             public Iterator<String> iterator();
          - each time we create a new iterator (remember, each iterator is unique)
       - look at alphabetIterable in IteratorExamples class in IteratorExamples code
          - now we can use an AlphabestSoup object in the
       - how could we write our own iterator for AlphabetSoup?
          - show SoupIterator class in IteratorExamples code

  • File I/O
       - File class (http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html)
          - represents a possible file or a directory on disk
             - that file or directory may NOT actually exist
          - what is a path?
             - all the subdirectories leading up to this file or directory
          - what's the difference between getPath() and getAbsolutePath()
             - an absolute path starts from the root directory (in our case '/')
             - getPath() returns the path relative to the directory the user is in (in Eclipse, it's within the workspace)
          - a number of useful commands
             - delete()
             - exists()
             - createNewFile()
             - isFile()
             - isDirectory()
             - listFiles()
             - mkdir()
             - renameTo(...)
          - what is a path separator?
          - why should we use File.pathSeparator vs. just putting in '/'?
       - Two common classes
          - PrintWriter out = new PrintWriter(new FileWriter(...));
          - BufferedReader in = new BufferedReader(new FileReader(...));
          - why the complicated statements?