Lec 20 More Arrays--Algorithms
Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem – So an array algorithm is a well-defined specification for solving a problem involving arrays – Ask class: What stuff would you like to see done using arrays?
Some examples of array algorithms (section 7.5 in the book) – ``Resizing'' arrays (not in book?) Actually, you can't resize an array, you just make a bigger array and copy everything over – Testing equality of arrays Don't use the equals method on arrays; it does the same thing as == in this case – Counting matches – Finding a value – Finding minimum/maximum – Copying arrays (section 7.7)
4 Quiz Draw the following arrays int [] data = new int[8]; for (k=0; k<8; k++) data[k]=k; int [] data2 = new int[8]; for (k=0; k<8; k++) data2[k]=k-5; int [] data3 = new int[8]; for (k=0; k<8; k++) data3[k]=7-k;
5 How to Swap ?? One of the most useful algorithms Swap two int variables: int x=3, y=5, temp; 1) temp=x; 2) x=y; 3) y=temp; for swapping array cells, replace x with data[k], replace y with data[m] in lines 1) to 3) xytemp ?333?333
Searching for a value in an array Suppose you want to know if 13 is in the data array. Naive approach: – for (int k=0; k<11; k++){ if (data[k]==13) output "found 13 at cell" + k else output "13 not found" } the problem ? if data[0] doesn't match, you can't tell after looking at one cell that it's not there
A solution? assume value 13 is not in the array – boolean found = false; visit each cell in the array, if there's a match – display "found 13 at cell " + k, – set found to true otherwise, do nothing (no else) but continue to check next cell After loop, if found is still false, say "not found"
Finding the max in the array: set max = 0 visit every cell using counter k – if data cell k is bigger than max set max to data cell value at position k set maxindex equal to k (to remember the k value) when the loop ends, max will have largest value in array, maxindex will tell you which cell it was in
Two-dimensional arrays (section 7.6, advanced topic 7.2) – We start with a couple of observations: 1) Every array is defined to store some particular kind of object (or primitive) 2) Any array itself is an object – From observations 1) and 2) we conclude: We can define an array that holds other arrays! Such an array is called a two-dimensional array – To see how the syntax works, see example next slideexample Also see memory diagram representing the state after the above code is executedmemory diagram See diagram for a different way to conceptualize 2D arraysdiagram – We can also generalize these ideas to any number of dimensions we want
public class TwoDimensionalArray { public static void main(String[] args) { //Declaring a two dimensional array, with initialization that //already creates the secondary arrays for you double[][] a = new double[3][2]; System.out.println(a[2][1]); //prints 0.0 //Now let's do it "by hand" double[][] b = new double[3][]; System.out.println(b[2]); //This will print null at this point b[0] = new double[2]; b[1] = new double[2]; b[2] = new double[2]; b[0][1] = 3.2; System.out.println(b[0][1]); //prints 3.2 }
Lab20 More Practice with Arrays You'll be creating an applet that uses arrays to store a number of words, and select and combine them randomly.