CS100J, Fall 2001 Tues 10/30 Lecture 17 ------------------------------------------------------------------------------- Announcements: + P5 due Tues 11/6 + T3 Tues 11/20 (see Exams-->Review 3 and Exams-->Prelim 3 in a week or so) + modified cutoffs for grades (will discuss in lecture) ------------------------------------------------------------------------------- Topics: + array of arrays (multidimensional arrays) + rectangular arrays + ragged arrays ------------------------------------------------------------------------------- Summary: + 1D arrays: type[] var = new var[size] + initializer list: type[] var = {val,val,...}; + anonymous array: new type[] {val,val,...} (can return this!) + objects with arrays + reminder of arrays of objects: - general pattern: type[] var; var = new type[size]; var[0] = new type(...); var[1] = new type(...); and so on - If the type is another array, you have an ARRAY OF ARRAYS ------------------------------------------------------------------------------- Array of Arrays: + also called MULTIDIMENSIONAL ARRAYS + why bother? data that relates to other data + ex) height vs weight for height of 5', gather weights for height of 5'1", gather weights etc other examples: tensors, finite elements, graph theory,.... + how to visualize? RECTANGULAR RAGGED (t) (r) + Java: model with array of arrays ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Declaration: + type[][].... var; - each [] refers to a dimension - think type[] [] ^^^^^^ ^^ class "array class" + examples of acceptable syntax in 2D case: int[][] x <-- usually this one in CS100 int[] x[] int x[][] ------------------------------------------------------------------------------- Creating rectangular arrays: + var = new type[size1][size2]... ex) Real world: Store table t of values: +---+---+---+ | | | | t = +---+---+---+ | | | | +---+---+---+ 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[_____][_____]; ^ ^ | | rows ----+ +--- columns a[0][0]=__; a[0][1]=__; a[0][2]=__; a[1][0]=__; a[1][1]=__; a[1][2]=__; Printing values: for (int row = _____ ; ______________ ; ____ ) { // rows for (int col = _____ ; ______________ ; ____ ) // cols System.out.print( __________________ ) ; // element _________________________________________ ; // skip line } How Does Java really store the values? a 1st array 2nd arrays ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Creating arrays using the "array of array" concept (rectangular or ragged): + type[][] var = new type[size][] <=== last size is BLANK! - "first" or "earliest" or "left" array + stores the references to another array + the refs start as default values of $null$ - "last" or "inner" or "right" array + needs to be created + the reference is stored in the "earlier" array + if the array is the LAST in a connection of arrays, then it stores the actual value + example: ragged array (see aoa0.java) - the "first" dimension must ALWAYS be specified for size - all other dimensions can be blank, starting from the "right" ex) new type[1][4][] is OK new type[2][][3] is BAD ------------------------------------------------------------------------------- 2 or more dimensions: + use more []s: type[][] var; <- 2D type[][][] var; <- 3D type[][][][] var; <- 4D + the "last" array is always the one to store the values ex) int[][][] x = new int[2][3][1]; (draw boxes) ex) see aoa3d.java -------------------------------------------------------------------------------