CS100J, Spring 2001 Tues 3/13 Lecture 15 ------------------------------------------------------------------------------- Announcements: + T2 on Thurs 3/15 + see Exams for rooms (same as last time) + P5 due on Thurs 4/5 (unchanged) ------------------------------------------------------------------------------- Topics: + Prelim 2 hints + $for$ + re-intro to arrays ------------------------------------------------------------------------------- Summary from Lecture 14 + OOP wrap-up + intro to arrays ------------------------------------------------------------------------------- Prelim 2 Hints: + Projects 3,4 and exercises! ------------------------------------------------------------------------------- $for$ loop: + for(initialization; test; increment) statements + initialization, test, and increment can multiple expressions separated by commas or be left blank + usually use for determinate loops - think pure repetition or known stopping conditions - otherwise you need to use $break$ (which is usually bad) + variables declared inside initialization are LOCAL to the scope of the $for$ loop + converting $while$ to $for$: int i = 1; while ( i < 4) { becomes for (int i = 1 ; i < 4 ; i++ ) System.out.println(i); System.out.println(i); i++; } ------------------------------------------------------------------------------- ARRAYS: + what are arrays, in general? - arrays are collections of items - think tables, spreadsheets - math definitions: see linear algebra + why arrays? - want to manipulate/move/handle many items - data storage - science/engineering models/experiments are discrete - programming: compound information of the same type cuts out a LOT of redundancy! ------------------------------------------------------------------------------- Math Notation: Imagine an experiment: i | x -----+----- 0 | 1 becomes an array x = [ 1 17 4 ] 1 | 17 2 | 4 + x is the ith element in array x i + i is the index or subscript + may also see {elements} or [elements] in math ------------------------------------------------------------------------------- Java's arrays: + arrays are objects! + all elements must have the same type (or class) + indicies must be integers or expressions that evaluate to integers + most basic array is a 1-D collection of items + will do mulitdimensional later ------------------------------------------------------------------------------- Creating arrays: + declare: type[] name type name[] + assign: name = new type[size] + shortcut version: type[] name = new type[size] (more "versions" show up later) ------------------------------------------------------------------------------- Important notes/features: + [] is operator and has highest precedence + can declare arrays in same statement but be careful: ex) int a[], b; -> b is NOT an array + why $new$? arrays are objects + size is number of elements (must be integer or integer expression >= 0) + labeling of indicies starts at zero! + if you attempt to access index that does not exist, Java complains with out-of-bounds exception (not all languages do this!) + can find length automatically with $arrayname.length$ + all values in array are "zeros" by default (just like instance variables) (not true for other languages!) ------------------------------------------------------------------------------- How to think of objects? + arrays "live" in $type[]$ class + draw a box to represent the array + draw boxes to represent individual elements + elements get initial default values ("zeros") + each element resembles a reference variable (helps when dealing with arrays of objects) + arrays have built-in $length$ "instance variable" ------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------