BOOK NOTES CHAPTER 8 - skip: 8.2, autoboxing, 8.4 8.1 ARRAYS - manipulate a collection of related values - impractical to use a sequence of variables - array: sequence of values of the same type - construction: new []; - length: number of elements in array - declare variable to hold array reference [] - type can be a primitive or an object - default values: - 0: numbers - false: booleans - null: objects - index: number used to reference a particular element [] - access particular locations (get and set) - indices start at 0 - index of last element is one less than array length - error to access nonexistent index: bounds error - length field returns length: .length - anomoly that not a method - cannot be changed - length is fixed - bounds errors - access a nonexistant element - error handling - causes exception and termination - much better than C/C++ - initialization - remember to initialize an array before trying to access it - initialize elements on creation - created array will have length of the number of elements specified - syntax: [] = {} new [] {} - length - String: a.length() - array: a.length 8.3 WRAPPERS - wrapper class: turn a primitive into an object - one for each primitive type: byte Byte boolean Boolean char Character double Double float Float int Integer long Long short Short - upper case letters - Character and Integer differ from primitive names - each object contains a value of the corresponding primitive type - used anywhere objects are required instead of primitives - create with a constructor: provide primitive as argument - assign object to primitive: use Value() method - storing wrapped numbers is quite inefficient 8.5 SIMPLE ARRAY ALGORITHMS 8.5.1 COUNTING MATCHES - count values in an array - check all elements - count the matches until you reach the end of the array 8.5.2 FINDING A VALUE - inspect each value - stop when - find match - reach end - aka linear search 8.5.3 FINDING THE MAXIMUM OR MINIMUM - initialize the candidate to the first element - keep a candidate for the maximum - if find larger, use to replace candidate - at the end of the array, you have the max 8.6 TWO-DIMENSIONAL ARRAYS - arrays store linear sequences - collection with 2D layout - declaration syntax [][] - creation syntax new [][] - access syntax [][] - use nested loops to access all elements - arrays with variable row lengths - allocation - allocate an array to hold all of the rows - don't specify their size - syntax: [][] = new [][]; - allocate each row separately [] = new []; - access - [][] - be especially careful that col is a valid index - uncommon - multidimensional arrays - arrays with more than two dimensions - 3D array definition [][][] = new [][][]; - especially rare in OO programming 8.7 COPYING ARRAYS - an array variable holds a reference to the actual array - copying the variable yields another reference to the same array - make a true copy - clone method - cast return type from Object to array type - copy elements from one array into another - static System.arraycopy(from, fromStart, to, toStart, count) - add or remove elements in the middle of an array - add a new element at position i - move all elements from i onword one position up System.arraycopy(data, i, data, i + 1, data.length - i - 1); - insert new value data[i] = x; - last element is lost - remove the element at i - copy all elements above downward System.arraycopy(data, i + 1, data, i, data.length - i - 1); - grow an array that has run out of space - create a new, larger array double[] newData = new double[2 * data.length]; - copy all elements into the new array System.arraycopy(data, 0, newData, 0, data.length); - store the reference to the new array in the array variable data = newData;