// APS101, Winter 2009: Lecture 24 (Mar. 10) // // Before we begin, let's review some Input/Output from yesterday's Lab 6. // (we wrote method FileAccess.displayFile) // // Also, let's briefly go over JavaDoc comments // (you will need to write these for A3) // // Need to use 2 special tags: - use @param to describe the parameters of a method Syntax: @param p_name1 comment @param p_name2 comment ... - use @return to specify the return type of a non-void method Syntax: @return comment * there should be only ONE @return tag for any non-void method (function) * // Review: last time we started talking about arrays. // // what are some advantages of arrays? // what are some disadvantages of arrays? // // we also wrote method ArrayDemo.displayArray // similar to String's substring method // // ex. on ONE line, declare an integer array of size 3, with // the following contents: {3, 2, 1}, and print out the 2nd and 3rd // elements of this array. ArrayDemo.displayArray(new int[] = {3, 2, 1}, 1, 2) // doesn't work! ArrayDemo.displayArray(int[] = {3, 2, 1}, 1, 2) // also doesn't work! ArrayDemo.displayArray(new int[]{3, 2, 1}, 1, 2) // recall that you can declare and initialize an array in 3 ways: // 1) int[] a = new int[3]; a[0] = 3; a[1] = 2; a[2] = 1; // 2) int[] a = new int[]{3, 2, 1}; // 3) int[] a = {3, 2, 1}; // what is the array equivalent of String's charAt method? // trick question: elements in an array are always retrieved using an index ex. a[0] // what is the array equivalent of String's indexOf method? // let's write our own: method findElement in ArrayDemo.java // (notice the difference in efficiency between Versions 1 and 2) ArrayDemo.findElement(new int[]{1, 2, 3}, 2) ArrayDemo.findElement(new int[]{1, 2, 3}, 3) ArrayDemo.findElement(new int[]{1, 2, 3}, 4) // returns -1! // how do you test whether 2 arrays have the same contents? int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; a == b // remember that a and b are objects... in different memory locations! // need to write our own method that tests equality of arrays // how do you copy the contents of one array to another? a = b // this doesn't copy contents of b into a! it copies reference of b to a! a == b // a and b point to the same array now a[0] = 10 a[0] b[0] // as you can see, arrays a and b are now the same!