CS62 - Spring 2011 - Lecture 10
Admin
- Assignment 2: how did it go?
- if you're not done, turn it in sooner than later
- Assignment 3: out soon... "on-disk" sorting
- what is main memory (or sometimes just memory?)
- what happens if we write a program that needs more than is available?
- A few random things
- keep up with the reading!
- read the assignments thoroughly before starting
- we don't explicitly do designs in this class, but think about how the pieces fit together
- been a bit lax about the practice problems because we've been covering odd topics, but we'll start again next week!
- Exercises
- e-mail me videos
File recap
-
http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
- the File class represents a POSSIBLE file or 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(...)
- Two common classes
- PrintWriter out = new PrintWriter(new FileWriter(...));
- BufferedReader in = new BufferedReader(new FileReader(...));
- why the complicated statements?
Lists (
http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html
)
- "An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list."
- Design an interface for list. What methods should you support?
- add to the end
- add at a particular index
- contains
- get
- set
- isEmpty
- size
- remove
- subList
- iterator
- The ArrayList class implements the List interface. Let's look at the runtime of the various methods:
- what operations are performed quickly by the ArrayList class?
- add to the end
- get
- set
- size
Linked lists
- the ArrayList class is built on top of an underlying array
- because of this, the get and set operations are efficient
- what operations are slow?
- inserting an element at a particular index
- removing an element
- searching
- where are we headed now?
- there's no one best data structure
- depending on what operations are important, one data structure may be more appropriate than another
- we're going to start looking at different data structures that are good at different things
- Today: linked lists are a recursive data structure
- the data structure is built out of nodes
- a node stores both a data item as well as a reference to another node
- draw a picture and compare and contrast a picture of an array (or an ArrayList)
- how could we do this using a class?
- look at the Node class in
LinkedList code
- two things that we keep track of (with private instance variables)
- private E data: the data that we're actually storing in the node
- private Node<E> nextElement: a link to the next node in our linked list
- we have methods that allow us to access and manipulate the data within the node
- as well as methods that allow us to traverse and manipulate the links
- how would I build a linked list with N data items
- start by creating a node (node1) with some data
- create a new node (node2) with some other data and point it's next element at node1
- create a new node (nod3) with some more data and point it's next element at nod2
- etc, up through nodeN
- in the end, we'll have nodeN->nodeN-1->...->node3->node2->node1
- We want to build a linked list data structure that supports similar operations to ArrayList. How would we do it?
- we'll just store the head of the linked list
- all of our operations will start at the head and then modify/traverse the list appropriately
- A few operations (look at
LinkedList code
)
- public void addFirst(E value)
- we need to associate head with a new value
- and set the "next" element for the new head to be the old head
- this is accomplished at the same time, using the Node constructor
- public E removeFirst()
- save the value of head so we can return it
- set head to be head.next()
- return the value of the original head
- public boolean contains(E value)
- start at the head and traverse through the nodes
- we can just keep a Node<E> reference and update it with .next() to move to the next node
- stop when we either find it or we run off the end