© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Chapter 6: Arrays & More GUI Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used.
Wecksten, Mattias Arrays Based on templates from Lewis & Loftus.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Chapter 6: Arrays Java Software Solutions Third Edition
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
© 2004 Pearson Addison-Wesley. All rights reserved October 17, 2007 Searching (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
© 2004 Pearson Addison-Wesley. All rights reserved October 19, 2007 Sorting ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/27 Outline Declaring and Using Arrays Arrays of Objects Variable Length.
Programming in Java (COP 2250) Lecture Chengyong Yang Fall, 2005.
1 Arrays An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 7: Arrays.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Two-Dimensional Arrays
Parameter Lists & Command Line Arguments
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Quick review of last lecture

© 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 one dimension two dimensions

© 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

© 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

© 2004 Pearson Addison-Wesley. All rights reserved Anatomy of a 2D Array [

© 2004 Pearson Addison-Wesley. All rights reserved Two-Dimensional Arrays ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer

© 2004 Pearson Addison-Wesley. All rights reserved Example of a regular 2D array [ Note: In Java the first index should be 0 not 1!

© 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

© 2004 Pearson Addison-Wesley. All rights reserved Example of a Ragged Array [ Note: In Java the first index should be 0 not 1!

© 2004 Pearson Addison-Wesley. All rights reserved Two-Dimensional Arrays ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer

© 2004 Pearson Addison-Wesley. All rights reserved 3D Array Example

© 2004 Pearson Addison-Wesley. All rights reserved 3D Arrays [

© 2004 Pearson Addison-Wesley. All rights reserved Other Stuff

© 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

© 2004 Pearson Addison-Wesley. All rights reserved Java Example: Printing an Array

© 2004 Pearson Addison-Wesley. All rights reserved ArrayList Class (Take a look at Section 7.7) (We will come back to it later)

© 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

© 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

© 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) Beatles.java 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 reunion = new ArrayList This is an example of generics, which are discussed further in Chapter 12

© 2004 Pearson Addison-Wesley. All rights reserved Example: Beatles.java (page 405) Beatles.java

© 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

Searching Not in the Textbook

© 2004 Pearson Addison-Wesley. All rights reserved Search [

© 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

© 2004 Pearson Addison-Wesley. All rights reserved Example: Successful Linear Search [

© 2004 Pearson Addison-Wesley. All rights reserved [ Example: Failed Linear Search

© 2004 Pearson Addison-Wesley. All rights reserved Java Example: Linear Search

© 2004 Pearson Addison-Wesley. All rights reserved Java Example: Finding the minimum number in an array of unsorted integers

© 2004 Pearson Addison-Wesley. All rights reserved THE END