06 - 17:40:30 1 06 CS 61B Lecture 6 Friday Today’s...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 09/09/10 17:40:30 1 06 CS 61B: Lecture 6 Friday, September 10, 2010 Today’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-level array references. This was necessary to construct a triangular array. But if you want a rectangular multi-dimensional array, rather than a triangular one, Java can create all of the arrays for you at once. int table = new int[x][y]; This declaration creates an array of x references to arrays. It also creates x arrays of y ints. The variable "table" references the array of arrays; and each entry in the array of arrays references one of the arrays of ints. All the arrays are created for you at once. Similarly, Java can create three- or ten-dimensional arrays for you, memory permitting. We could have used a square array to store Pascal’s Triangle, but that would have unnecessarily wasted memory. If you have enough memory, you might not care. When you declare a variable, you can also create array entries by using initializers. Human b = {kayla, 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. Sadly, you can only use this notation in a declaration. You can’t write d = {3, 7}; // Compile-time ERROR. f({1, 2, 3}); // Compile-time ERROR. 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 create a multi-dimensional array, Java can create all the arrays for you. But when you create an array of objects, Java does not create the objects automatically. The array contains space for references to the objects. You must create the objects yourself. String sentence = new String[3]; sentence[0] = "Word"; sentence[2] = new String();--- -------------------- --- sentence |.+----->| . | null | .--+---->| |--- ---+---------------- --- empty String | | -------- \---->| Word |-------- main()’s Parameter------------------...
View Full Document

This note was uploaded on 01/10/2012 for the course CS 61B taught by Professor Canny during the Fall '01 term at Berkeley.

Page1 / 2

06 - 17:40:30 1 06 CS 61B Lecture 6 Friday Today’s...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online