Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lec 20 More Arrays--Algorithms

2 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?

3 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 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 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 355355 5353 ?333?333

6 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

7 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"

8 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

9

10 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

11 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 }

12

13

14 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.


Download ppt "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."

Similar presentations


Ads by Google