© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming...
-
Upload
kelley-obrien -
Category
Documents
-
view
219 -
download
0
Transcript of © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming...
![Page 1: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/1.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
October 15, 2007
Searching
ComS 207: Programming I (in Java)Iowa State University, FALL 2007Instructor: Alexander Stoytchev
![Page 2: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/2.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Quick review of last lecture
![Page 3: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/3.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Two-Dimensional Arrays
• A one-dimensional array stores a list of elements
• A two-dimensional array can be thought of as a table of elements, with rows and columns
onedimension
twodimensions
![Page 4: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/4.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Two-Dimensional Arrays
• To be precise, in Java a two-dimensional array is an array of arrays
• A two-dimensional array is declared by specifying the size of each dimension separately:
int[][] scores = new int[12][50];
• A array element is referenced using two index values:
value = scores[3][6]
• The array stored in one row can be specified using one index
![Page 5: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/5.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Arrays in Java
• Java represents 2D arrays as an array of arrays!
• In other words, a 2D integer array is really a 1D array of references to 1D integer arrays.
• The concept generalizes to N-dimensions
![Page 6: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/6.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Anatomy of a 2D Array
[http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm]
![Page 7: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/7.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Two-Dimensional Arrays
Expression Type Description
table int[][] 2D array of integers, or
array of integer arrays
table[5] int[] array of integers
table[5][12] int integer
![Page 8: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/8.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example of a regular 2D array
[http://livedocs.macromedia.com/coldfusion/5.0/Developing_ColdFusion_Applications/arrayStruct2.htm]
Note: In Java the first index should be 0 not 1!
![Page 9: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/9.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Multidimensional Arrays
• An array can have many dimensions – if it has more than one dimension, it is called a multidimensional array
• Each dimension subdivides the previous one into the specified number of elements
• Each dimension has its own length constant
• Because each dimension is an array of array references, the arrays within one dimension can be of different lengths
these are sometimes called ragged arrays
![Page 10: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/10.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example of a Ragged Array
[http://livedocs.macromedia.com/coldfusion/5.0/Developing_ColdFusion_Applications/arrayStruct2.htm]
Note: In Java the first index should be 0 not 1!
![Page 11: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/11.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Two-Dimensional Arrays
Expression Type Description
table int[][] 2D array of integers, or
array of integer arrays
table[5] int[] array of integers
table[5][12] int integer
![Page 12: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/12.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
3D Array Example
![Page 13: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/13.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
3D Arrays
[http://web.ics.purdue.edu/~cs154/lectures/lecture017.htm]
![Page 14: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/14.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Other Stuff
![Page 15: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/15.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Arrays as Parameters
• An entire array can be passed as a parameter to a method
• Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other
• Therefore, changing an array element within the method changes the original
• An individual array element can be passed to a method as well, in which case the type of the formal parameter is the same as the element type
![Page 16: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/16.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Java Example: Printing an Array
![Page 17: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/17.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
ArrayList Class
(Take a look at Section 7.7)(We will come back to it later)
![Page 18: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/18.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
The ArrayList Class
• The ArrayList class is part of the java.util package
• Like an array, it can store a list of values and reference each one using a numeric index
• However, you cannot use the bracket syntax with an ArrayList object
• Furthermore, an ArrayList object grows and shrinks as needed, adjusting its capacity as necessary
![Page 19: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/19.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
The ArrayList Class
• Elements can be inserted or removed with a single method invocation
• When an element is inserted, the other elements "move aside" to make room
• Likewise, when an element is removed, the list "collapses" to close the gap
• The indexes of the elements adjust accordingly
![Page 20: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/20.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
The ArrayList Class
• An ArrayList stores references to the Object class, which allows it to store any kind of object
• See Beatles.java (page 405)
• We can also define an ArrayList object to accept a particular type of object
• The following declaration creates an ArrayList object that only stores Family objects
ArrayList<Family> reunion = new ArrayList<Family>
• This is an example of generics, which are discussed further in Chapter 12
![Page 21: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/21.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example: Beatles.java (page 405)
![Page 22: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/22.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
ArrayList Efficiency
• The ArrayList class is implemented using an underlying array
• The array is manipulated so that indexes remain continuous as elements are added or removed
• If elements are added to and removed from the end of the list, this processing is fairly efficient
• But as elements are inserted and removed from the front or middle of the list, the remaining elements are shifted
![Page 23: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/23.jpg)
Searching
Not in the Textbook
![Page 24: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/24.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Search
[http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm]
![Page 25: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/25.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Linear Search
• The most basic
• Very easy to implement
• The array DOESN’T have to be sorted
• All array elements must be visited if the search fails
• Could be very slow
![Page 26: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/26.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example:SuccessfulLinear Search
[http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm]
![Page 27: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/27.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved [http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm]
Example:FailedLinear Search
![Page 28: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/28.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Java Example: Linear Search
![Page 29: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/29.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Java Example: Finding the minimum number in an
array of unsorted integers
![Page 30: © 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649f435503460f94c63656/html5/thumbnails/30.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
THE END