|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Quiz or Exam coming up: Apr.02 (before the Withdraw-deadline)
We have seen LinkedLists, as a great way to store any number of items. To process the contents of a list, we use for-each:
java.util.LinkedList<Double> dubs = new java.util.LinkedList<Double>(); // ... add items to dubs double sumSoFar = 0.0; for ( Double dd : dubs ) { sumSoFar = sumSoFar + dd.doubleValue(); } // At this point, sumSoFar contains the sum of all numbers in dubs. |
java source, all files (source), all files (.jar).
import java.util.LinkedList; // The only time you don't use angle-brackets. Dog d1 = new Dog(); Dog d2 = new Dog(); Dog d3 = new Dog(); d2.ageOneYear(); LinkedList<Dog> doggies = new LinkedList<Dog>(); doggies.add(d1) doggies.add(d2) doggies.add(d3) // Draw a picture. // (How many Dog objects exist? How many LinkedLists?) // How many variables?) doggies.add(d2) doggies.toString() d2.ageOneYear(); doggies.toString() |
reminder: Sometimes people confuse the variable referring to the list, with the loop-variable which refers to an element of the list. They are very different!
You don't confuse an apple with a bag-of-apples; you don't confuse a bus-passenger with a bus; so similarly don't confuse an item with a collection-of-items.
Given a list of Dogs, …
Here's a method which gives a directory-listing: It gives a list of files (and their sizes), for each file inside a directory.
/** Return The sum of the length of each file immediately inside directoryName. * Doesn't recur into any sub-directories (but does include any size a directory * itself takes up, aside from its sub-contents). * @param directoryName The pathname of a directory. * @return The sum of the length of each file immediately inside directoryName. */ public long directoryListing( String directoryName ) { // Convert the directoryName into a File object, so we can get its size etc.: File theDirectory = new File( directoryName ); // import java.io.File at the top of this file, so we don't need to use its full name. long sizeSoFar = 0; // Accumulate the sum of all immediate file sizes. for ( File f : theDirectory.listFiles() ) { // For each `f' in the directory, sizeSoFar = sizeSoFar + f.length(); // Add f's length to our sum. } return sizeSoFar; } |
We will work through one or two of these problems in class; you should use the others as practice.
(Really, it'd be more appropriate to return a list of java.io.Files; that way whoever calls our method can do what they want with the files -- whether it is just get their filenames, or something fancier like moving them to a different directory. (You are strongly advised not to write any programs which try to modify your disk files at all!) )
2There are also two other steps, to be technically correct: (0) the invariant holds before the loop even begins, and (3) the loop will eventually halt. ↩
1When trying to convince somebody your loop is correct, there are two important2 steps: (1) In the case that the loop isn't done yet, argue that the loop-invariant being true before the loop begins implies that the loop-invariant holds again after the entire body finishes. (2) In the case that the loop is finished, then the loop invariant is enough to prove that you have computed what you claimed to compute. Although rarely do other people ask you to show these facts about your loops, it does help you understand your loop, to be forced to write down your loop invariant. ↩
4 It's usually poor style to write a loop which needs to refer to this last value. (For example, if your code goes back and un-does the last iteration of the loop body, this is sloppy. It also is problematic when the loop body is never performed even once (such as when you have an empty list).) ↩
3 Technically, you can write a for loop where you don't declare a new local variable, and instead mention an existing variable to hold the loop value. In that case, that variable still exists after the loop finishes, and it holds the4 the most recent value through the loop. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |