190 likes | 309 Views
CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu. Agenda. Announcements Cell phones / laptops off & away / Name signs out Last time type conversions green UML class diagram tool demo Today searching. Search.
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu
Agenda • Announcements • Cell phones / laptops off & away / Name signs out • Last time • type conversions • green UML class diagram tool demo • Today • searching
Search • Computers are good at storing large amounts of data • Finding a particular datum in a large collection is a typical operation • TODAY • how to search for a value in an unordered collection • determining the smallest/largest value in a collection
general search • General problem: determine whether a value exists in a given collection • Assumption: to make things easier we assume that the collection contains Strings • Approach: define a method which accepts as arguments a Collection<String> and a value of type String, and returns a boolean indicating whether the value is in the collection
steps in defining method Step 1 – stub out the method public booleanisMemberOf(String s, Collection<String> c){ return false; }
steps in defining method Step 2 – set up loop Can use any of while/for/for-each. public booleanisMemberOf(String s, Collection<String> c){ for (String s : c) { } return false; }
steps in defining method Step 3 – set up test in body of loop public booleanisMemberOf(String s, Collection<String> c){ for (String x : c) { if (s.equals(x)) { } } return false; }
steps in defining method Step 4 – but we need to be careful, as s could be null! Relies on short-circuit evaluation of boolean operators. public booleanisMemberOf(String s, Collection<String> c){ for (String x : c) { if ((s==null) && (x==null) || s.equals(x)) { } } return false; }
steps in defining method Step 5 – If we found the value, return true. If we search the entire collection without finding the value, return false public booleanisMemberOf(String s, Collection<String> c){ for (String x : c) { if ((s==null) && (x==null) || s.equals(x)) { // we found the value --- return true return true; } } // we searched entire collection // but did not find the value --- return false return false; }
wrapper types • Collections can hold only references to objects. • Values of primitive types cannot be put into collections directly. • Each primitive type has an associated wrapper class:
wrapper types • Each instance of a wrapper class holds a primitive value. • Examples: inti = 3; Integer wi = new Integer(i); // create wrapper int y = wi.intValue(); // get int value double d = 3.5; Double wd = new Double(d); // create wrapper double z = wd.doubleValue(); // get double value
autoboxing/unboxing • In many cases the compiler can insert an automatic conversion to (boxing) or from (unboxing) a wrapper class object. • Examples: Collection<Integer> c = new ArrayList<Integer>(); c.add(4); // equivalent to c.add(new Integer(4)); Integer wx = new Integer(3); Integer wy = new Integer(4); intpz; pz = wx + wy; // equivalent to: // pz = wx.intValue() + wy.intValue(); Integer wz; wz = wx + wy; // equivalent to: // wz = new Integer(wx.intValue() + wy.intValue());
finding the max • General problem: determine the maximum value in a collection • Assumption: to make things easier we assume that the collection contains non-null Integers, AND that the collection is not empty. • Approach: define a method which accepts as argument a Collection<Integer> and returns the smallest value is in the collection
steps in defining method Step 1 – stub out the method public Integer maximum(Collection<Integer> c){ return 0; }
steps in defining method Step 2 – Take first value of collection as a candidate for the maximum value. Extract first value using an iterator. public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe // FIND THE MAXIMUM, STORING IT IN ‘candidate’ return candidate; // return the candidate }
steps in defining method Step 3 – set up iterator-controlled loop to look at all values in collection public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe while (it.hasNext()) { // FIND THE MAX } return candidate; // return the candidate }
steps in defining method Step 4 – extract a vale from the collection public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe while (it.hasNext()) { Integer temp = it.next(); // extract next value from collection } return candidate; // return the candidate }
steps in defining method Step 5 – if it’s larger than current candidate, update candidate public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe while (it.hasNext()) { Integer temp = it.next(); // extract next value from collection if ( temp > candidate ) { // if we found a larger value candidate = temp; // update candidate } } return candidate; // after loop completes, candidate contains // the largest value in the collection }
minimum • Follow same basic steps as for finding maximum. • Change test (temp > candidate) • to (temp < candidate)