CS100J, Fall 2001 Tues, 10/23 Lecture 15 ------------------------------------------------------------------------------- Announcements: + reading $for$ on Thurs and beyond: 167-175 arrays on Thurs-next week: 6.1-6.3, 6.5 (6.4 happens the following week) + P5 posted + E4 due Thurs 10/25 ------------------------------------------------------------------------------- Topics: + what's an array?!? (why bother?) + 1D arrays in Java + default values + initializer lists + $for$ ------------------------------------------------------------------------------- ARRAYS: + what are arrays, in general? - arrays are collections of items - think of tables and spreadsheets - math definitions? see linear algebra (MATH 294, CS280) + 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 ] (1D array) 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 + when we say ARRAY, we mean 1D array in Java ------------------------------------------------------------------------------- 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 (after the $.$) + 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 1D arrays? + 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" + all elements of the array have default values of "zeros" (like fields) example of scope diagrams) int[] x = new int[3]; ------------------------------------------------------------------------------- example with code) public class array_basics { public static void main(String[] args) { // Make 3-element 1D array: int[] x = new int[___]; // Store 2, 4, and 6 in 1st, 2nd, and 3rd positions: // Print the array: print1D(_______); } // Print a 1D array horizontally: public static void print1D(________) { int col = 0; while (__________________) { System.out.print(_______________________ ); _________________ ; } System.out.println(); // why need this? } } // output: ------------------------------------------------------------------------------- Initializer lists: + handy way to creating an array and storing small amount of values: type[] var = {val,val,...}; <- the $;$ is mandatory here! ^^^^^^^^^^^^^^ INITIALIZER LIST + essentially a shortcut, but not useful as a trick to "pass arrays": $return {1, 2, 3}$ won't work + formal way to write as an anonymous array: typ[] var = new type[] {list of values}; ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ANONYMOUS ARRAY + may use anonymous arrays to pass a ref to an array WITH values ------------------------------------------------------------------------------- $for$ loop: + arrays involve lots of repetitive analysis + gets tiring entering $while$ all the time, so use $for$ + syntax: for(initialization; test; increment) statements + initialization, test, and increment can multiple expressions separated by commas or be left blank + 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++; } -------------------------------------------------------------------------------