LECTURE NOTES CHAPTER 8 Skip: 8.2, autoboxing, 8.4 ARRAYS - manipulate a collection of related values - impractical to use a sequence of variables - array - special kind of object - sequence of values of the same type - construction: new []; - length: number of elements in array Example: new int[7]; // Store daily low temps for this week - declare variable to hold array reference [] - type can be a primitive or an object - think of [] as an "array type" - what's an example that we've seen? - main method actually takes a list of String arguments - user can supply these when executing your program from a command line Example: int[] lowTemps; - default values: - 0: numbers - false: booleans - null: objects - index - number used to reference a particular element - indices start at 0 - index of last element is one less than array length - access an element of the array [] Example: int lowSunday = temp[0]; ask them --> int lowSaturday = temp[6]; int lowSaturday = temp[7]; - bounds errors - access a nonexistant element - error handling: exception and termination - length field returns length: .length - anomoly that not a method - final: cannot be changed - length is fixed - remember: - String: a.length() - array: a.length Example: // Call doSomething on every element of array for (int i = 0; i < array.length; i++) { doSomething(array[i]); } - 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 [] {} Example: 1) Suppose weekly lows are: 27, 28, 32, 17, 25, 13, 19. Make an array weekLows with these values: int[] weekLows = {27, 28, 32, 17, 25, 13, 19}; 2) A static Weather method takes an array of ints and calculates the average. Call this method with weekLows, without storing the values in a variable. Weather.average(new int[] {27, 28, 32, 17, 25, 13, 19}); 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 TWO-DIMENSIONAL ARRAYS - arrays store linear sequences - collection with 2D layout - declare an array where each element is another array - declaration syntax [][] - creation syntax new [][] - access syntax [][] - use nested loops to access all elements Example: int dim = ... double[][] prod, mul1, mul2; prod = new double[dim][dim]; mul1 = new double[dim][dim]; mul2 = new double[dim][dim]; // populate mul1 and mul2 // matrix multiply for (int i = 0; i < dim; i++) { for (j = 0; j < dim; j++) { prod[i][j] = 0; for (k = 0; k < dim; k++) { prod[i][j] += mul1[i][k] * mul2[k][j]; } } } Picture - 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 Example: int[][] march = new int[5][]; march[0] = new int[5]; march[1] = new int[7]; march[2] = new int[7]; march[3] = new int[7]; march[4] = new int[5]; for (int i = 0; i < march.length; i++) { for (int j = 0; j < march[i].length; j++) { // do something } } Picture MULTIDIMENSIONAL ARRAYS - arrays with more than two dimensions - cube array definition [][][] = new [][][]; - especially rare in OO programming Example: Molecule[][][] = new Molecule[][][]; COPYING ARRAYS - an array variable holds a reference to the actual array - copying the variable yields another reference to the same array Picture - make a true copy - clone method - cast return type from Object to array type Picture - 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 Picture - 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; Picture SIMPLE ARRAY ALGORITHMS - ArrayAlgorithms.java - counting matches - linear search - finding the maximum - finding the minimum