CS100J, Spring 2001
Thurs 3/15
Lecture 16
-------------------------------------------------------------------------------
Announcements:
+ T2 tonight, same rooms
+ P5 due on Thurs 4/5 (unchanged)
+ E7 3/27 (see web)
-------------------------------------------------------------------------------
Topics:
+ initializer lists
+ anonymous arrays
+ objects with arrays
+ arrays of objects (leads into multidimensional arrays, but not this time)
-------------------------------------------------------------------------------
Summary from Lecture 15
+ intro to arrays
+ 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
-------------------------------------------------------------------------------
Initializer lists:
+ handy way to creating an array and storing small amount of values:
  type[] var = {val,val,...}; <- the $;$ is mandatory here!
+ essentially a shortcut, but not useful as a trick to "pass arrays":
  return {1, 2, 3} 
  won't work
+ more formal way to write:
  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
-------------------------------------------------------------------------------
Objects with arrays:
+ some classes may have instance/class variables as arrays
+ create arrays at instance variable or in method/constructor
+ remember that array size must not change once set
+ see classWithArrays.java
-------------------------------------------------------------------------------
Arrays with objects:
+ type[] implies all kinds of types
  primitive types, class names!
+ 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
+ $[]$ has higher precedence than $.$
  ex) Blah b = new Blah[2]; 
      b[0].x = 3; <- finds the 1st element of $b$
                     then finds that object's var $x$ and assigns new value
+ see arrayOfObjects.java
-------------------------------------------------------------------------------