02/10/1415:19:54 106 CS 61B: Lecture 6 Monday, February 3, 2014Today’s reading: Sierra & Bates pp. 282-285.MORE ARRAYS===========Automatic Array Construction----------------------------Last lecture, we used a loop to construct all the arrays that the top-levelarray references. This was necessary to construct a triangular array. But ifyou want a rectangular multi-dimensional array, rather than a triangular one,Java can construct all of the arrays for you at once. int[][] table = new int[x][y];This declaration constructs an array of x references to arrays. It alsoconstructs x arrays of y ints. The variable "table" references the array ofarrays; and each entry in the array of arrays references one of the arrays ofints. All the arrays are constructed for you at once. Similarly, Java canconstruct three- or ten-dimensional arrays for you, memory permitting.We could have used a square array to store Pascal’s Triangle, but that wouldhave unnecessarily wasted memory. If you have enough memory, you might notcare.When you declare a variable, you can also construct array entries by usinginitializers. Human[] b = {amanda, rishi, new Human("Paolo")}; int[][] c = {{7, 3, 2}, {x}, {8, 5, 0, 0}, {y + z, 3}};In the second example, Java constructs a non-rectangular two-dimensional array,composed of one array of arrays and four arrays of ints.Outside of declarations, you need a more complicated notation. d = new int[] {3, 7}; f(new int[] {1, 2, 3});Another subtlety of array declarations is the following. int[] a, b, c; // a, b, and c all reference arrays. int a[], b, c[][]; // a is 1D; c is 2D; b is not a reference/array. int[] a, b[]; // a references a 1D array; b references a 2D array.Arrays of Objects-----------------When you construct a multi-dimensional array, Java can construct all the arraysfor you. But when you construct an array of objects, Java does not constructthe objects automatically. The array contains space for references to theobjects. You must construct the objects yourself. String[] sentence = new String[3]; sentence[0] = "Word"; sentence[2] = new String(); --- -------------------- --- sentence |.+----->| . | null | .--+---->| | --- ---+---------------- --- empty String | | -------- \---->| Word | --------main()’s Parameter------------------What is the array of Strings that the main() method takes as a parameter?It’s a list of command-line arguments sent to your Java program, prepared foryou by Java. Consider the following program. class Echo { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } } }If we compile this and type "java Echo kneel and worship Java", java prints kneel --- --------------------------------- and args |.+---->| . | . | . | . | worship --- ----+-------+-------+-------+---- Java | | | | v v v vMORE LOOPS ------- ----- --------- ------========== |kneel| |and| |worship| |Java| ------- ----- --------- ------"do" Loops----------A "do" loop has just one difference from a "while" loop. If Java reachesa "do" loop, it _always_ executes the loop body at least once. Java doesn’tcheck the loop condition until the end of the first iteration. "do" loops areappropriate for any loop you always want executed at least once, especially ifthe variables in the condition won’t have meaningful assignments until the loopbody has been executed. do { s = keybd.readLine(); process(s); } while (s.length() > 0); // Exit loop if s is an empty String.The "break" and "continue" Statements-------------------------------------A "break" statement immediately exits the innermost loop or "switch" statementenclosing the "break", and continues execution at the code following the loopor "switch".In the loop example above, we might want to skip "process(s)" when s is asignal to exit (in this case, an empty String). We want a "time-and-a-half"loop--we want to enter the loop at a different point in the read-process cyclethan we want to exit the loop at. Here are two alternative loops that do theright thing. They behave identically. Each has a different disadvantage. s = keybd.readLine(); | while (true) { // Loop forever. while (s.length() > 0) { | s = keybd.readLine(); process(s); | if (s.length() == 0) { s = keybd.readLine(); | break; } | } | process(s); Disadvantage: The line "s = keybd..." | } is repeated twice. It’s not really | a disadvantage here, but if input | Disadvantage: Somewhat obfuscated fortook 100 lines of code, the | the reader, because the loop isn’t duplication would make the code harder | aligned with its natural endpoint. to maintain. Why? Because aprogrammer improving the code might change one copy of the duplicated codewithout noticing the need to change the other to match.02/10/1415:19:54 206Some loops have more than one natural endpoint. Suppose we want to iterate theread-process loop at most ten times. In the example at left below, the "break"statement cannot be criticized, because the loop has two natural endpoints. Wecould get rid of the "break" by writing the loop as at right below, but theresult is longer and harder to read. for (int i = 0; i < 10; i++) { | int i = 0; s = keybd.readLine(); | do { if (s.length() == 0) { | s = keybd.readLine(); break; | if (s.length() > 0) { }
View Full Document