CS100J, Fall 2001 Thurs 10/25 Lecture 16 ------------------------------------------------------------------------------- Announcements: + E5: 1-2 page algorithm for P5, due Tues + P5: actual due date (not 11/13).... + future reading: 6.4 for next Thurs; next, inheritance: 7.1-7.3 ------------------------------------------------------------------------------- Topics: + initializer lists + anonymous arrays + objects with arrays + arrays of objects (leads into multidimensional arrays, but not this time) ------------------------------------------------------------------------------- Summary from Lecture 15 + Arrays: collections of data of type; another kind of object in Java + array syntax: type[] var = new type[size] indexing: var[index] assigning: var[index] = value default values: elements are "zeros" instance variable: var.length + $for$ for(init;check;incr) statements use for determinite loops ------------------------------------------------------------------------------- Initializer lists: + handy way to creating an array and storing small amount of values: type[] var = {val,val,...}; <- the $;$ is mandatory here! ^^^^^^^^^^^^^^ INITIALIZER LIST + MUST declare on the same line! + essentially a shortcut ex) write an init list that does the following statements: int[] x = new int[2]; x[0]=10; x[1]=20; + cannot change the size! + initializer list not useful as a trick to "pass arrays": $return {1, 2, 3}$ won't work ------------------------------------------------------------------------------- Anonymous array: + formal way to write as an anonymous array: typ[] var = new type[] {list of values}; ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ANONYMOUS ARRAY + may use anonymous arrays to pass a ref to an array WITH values ex) $return new int[] {1,2,3}$ will actually return a ref to an array from a method $return {1,2,3}$ will cause compiler error ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- public class anonarray0 { public static void main(String[] args) { double[] r = randomArray(); for (int i = 0; i< r.length; i++) System.out.print(r[i] + " "); System.out.println(); } // Generate an array of three values: private static ____________ randomArray() { return _______________ { Math.random(),Math.random(),Math.random() }; } } ------------------------------------------------------------------------------- Objects with arrays: + one way of "faking" a collection of different types + some classes may have fields as arrays + how? - create arrays directly in field - create arrays in a constructor and/or method + remember: array size must not change once the array is created see classWithArrays.java ------------------------------------------------------------------------------- Arrays with objects: + think of the syntax type[] - this syntax implies all kinds of types! - can be primitive types, can be classes? + examples: Worker[], Tray[], Blah[], etc + array of objects means ARRAY OF REFERENCES - elements hold references, not objects! - need to create objects for each element of array - default values are null + to access members of objects "in" an array (the refs are in the array), - use name[index].member - the $[]$ "operate" before the $.$ see personArray.java, arrayOfObjects.java -------------------------------------------------------------------------------