{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 06 - 05:34:22 CS 61B Lecture 6 Monday February 2 2009...

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

02/01/09 05:34:22 1 06 CS 61B: Lecture 6 Monday, February 2, 2009 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. Of course, 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 ------------------ What is the array of Strings that the main() method takes as a parameter?

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern