CS100J, Fall 2001 Tues 10/30 Lecture 18 ------------------------------------------------------------------------------- Announcements: + P5 due Tues 11/6 + T3 Tues 11/20 (see Exams-->Review 3 and Exams-->Prelim 3 in a week or so) + ------------------------------------------------------------------------------- Topics: + array of arrays (multidimensional arrays, 3d now) + row major vs col major + searching techniques ------------------------------------------------------------------------------- Summary: + rectangular multidim arrays: type[][]... var = new var[size1][size2].... + first array, second array, .... + raggedness ------------------------------------------------------------------------------- Creating arrays using the "array of array" concept (rectangular or ragged): + 2D: 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 + MultiD: (see example of ragged array in aoa0.java) - the "first" dimension must ALWAYS be specified for size - all other dimensions can be blank, starting from the "right" - no dimensions inbetween those with sizes can be blank 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 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Row Major vs Column Major: + the programmer may store values in any order + the programming must then choose how to access those values + Java does not think in terms of rows and columns, just in terms of "first" and "second" and ... arrays + Row Major: organize rows of ACTUAL array as rows of Java arrays Math Java +-------+ | 1 2 3 | | 4 5 6 | +-------+ + Col Major: organize columns of ACTUAL array as "columns" of Java arrays Math Java +-------+ | 1 2 3 | | 4 5 6 | +-------+ + How does Java ACTUALLY store things? - everything is a column! - stored from bottom to top - "physically invarient" ------------------------------------------------------------------------------- Printing a row-major matrix: ALGORITHM: start a new line (if necessary) for each row, pick each column and print each element start a new line ------------------------------------------------------------------------------- Printing a col-major matrix: ALGORITHM: start a new line (if necessary) for each row, pick each column and print each element start a new line More detail: maxDepth <- max column depth (the number of rows) maxCols <- "length" of first array (the number of columns) for each row that does not exceed maxDepth for each col that does not exceed maxCols if an element exists in the column print the element with position col,row start a new line -------------------------------------------------------------------------------