CS100J, Spring 2001
Tues 3/27
Lecture 17
-------------------------------------------------------------------------------
Announcements:
+ P4, T2 stuff posted
+ ProgramLive finally updated (thanx Kanghoe)
+ P5 due 4/5
-------------------------------------------------------------------------------
Topics:
+ multidimensional arrays
-------------------------------------------------------------------------------
Summary from Lecture 16
+ initializer list: type[] var = {val,val,...};
+ anonymous array:  new type[] {val,val,...} (can return this!)
+ objects with arrays
+ arrays of objects (array stores refs to objects)
-------------------------------------------------------------------------------
arrays of objects:
+ general pattern:
  type[] var;
  var = new type[size];
  var[1] = new type(...);
  var(2) = new type(...);
  and so on
+ what if type[] is another array?
+ called array of arrays
-------------------------------------------------------------------------------
array of arrays:
+ also called multidimensional arrays

declaration:
+ type[][].... var;
  - each [] refers to a dimension
  - think type[] []
          ^^^^^^ ^^
          class  array
+ examples of acceptable syntax in 2D case:
  int[][] x <-- usually this one in CS100
  int[] x[] 
  int x[][]

typical assignment (other approaches to come):
+ var = new type[size1][size2]... 
+ creates "rectangular" object 
+ $var$ refers to a MULTIDIMENSIONAL ARRAY
  - the "earlier" array contains refs to "later" arrays
  - the actual values are stored in the "last" array
+ "typical" means ROW MAJOR (demonstrated below)

ex) Real world:

    Store table t of values 
   
        +---+---+---+
        | 1 | 2 | 3 |
    t = +---+---+---+
	| 4 | 5 | 6 |
	+---+---+---+

    Math:
            Notation:  
    t       i -> row index       t -> entire array (also called matrix)
     ij     j -> column index    t -> specific element of t at position (i,j)
                                  ij

    Java: Without thinking about array of arrays:

          int[][] a = new int[2][3];
                              ^  ^
                              |  |
                       row ---+  +--- col
  
          a[0][0]=1; a[0][1]=2; a[0][2]=3;  
          a[1][0]=4; a[1][1]=5; a[1][2]=6;  

    Java: Now thinking about array of arrays:
          How does Java store the values?

          (draw boxes)
          a        1st array          2nd arrays












    Java: More detailed construction to produce same effect.
	  Uses an array of arrays concept:

          int[][] a = new int[2][];
	  a[0] = new int[3];
	  a[1] = new int[3];
          a[0][0]=1; a[0][1]=2; a[0][2]=3;  
          a[1][0]=4; a[1][1]=5; a[1][2]=6;  
 
	  Why $new int[2][]$? 
          + The "first" array contains ANY reference, including $null$! 
          + So, the outermost array never needs to be specified.
          + the "second" (or "outermost") array actually stores the values!

          Could have also said:
	  a[0]=new int[] {1, 2, 3}; 
	  a[1]=new int[] {4, 5, 6}; 
          Use anonymous arrays because initializer list syntax dictates
	  that declaration and assignment must be in the same statement!
-------------------------------------------------------------------------------
More than 2 Dimensions:
+ use more []s:
  type[][][] var; <- 3D
  type[][][][] var; <- 4D
+ the "last" array is always the one to store the values
-------------------------------------------------------------------------------
Ragged arrays:
+ "outer" arrays can contain refs to differently sized arrays
+ that's the reason for allowing the "last" dimension unspecified

  int[][] x = new int[2][];
  x[0] = new int[] {1};
  x[1] = new int[] {1,2};
-------------------------------------------------------------------------------
Row major vs column major:
+ ROW MAJOR: store rows of array
+ COL MAJOR: store cols of array
+ matter of bookkeeping
  - Java doesn't care how you store arrays
  - you are responsible for programming the correct indicies
  - the "major" business is a way of explaining useful storage methods

Row major:
 a = [row 0] = [1 2 3]
     [row 1]   [4 5 6]

Col major:
 a = [col0 col1 col2]
      [1]  [2]  [3]
      [4]  [5]  [6]
-------------------------------------------------------------------------------