320 likes | 392 Views
Problem Of The Day. Two missiles speed directly toward each other One goes 9,000 miles per hour Other goes 21,000 miles per hour. If they start 1,317 miles apart, how far apart are they 1 minute before colliding?. Problem Of The Day. Two missiles speed directly toward each other
E N D
Problem Of The Day • Two missiles speed directly toward each other • One goes 9,000 miles per hour • Other goes 21,000 miles per hour. • If they start 1,317 miles apart, how far apart are they 1 minute before colliding?
Problem Of The Day • Two missiles speed directly toward each other • One goes 9,000 miles per hour • Other goes 21,000 miles per hour. • If they start 1,317 miles apart, how far apart are they 1 minute before colliding? They are closing at 30,000MPH ; 1 minute of this is:30,000/60 = 3000/6 = 500 miles!
CSC 212 – Data Structures Lecture 27:List, Comparable AND COMPARATOR
Still Limited with Deque • Often want to use entire collection • Add element before existing one in the list • Find if element stored anywhere in the list • Loop over all elements without removing them • All these actions are impossible with Deque
Who Can Save Us From This? • What a great solution – we can use a List!
We Don’t Need No Stinking Elements • Note that ADT does not include methods to add • Clear sign more code used to make idea usable • Until we add elements, other methods not usable • But how to store elements in the List? • Store & maintain elements in order within List -or- • Listunordered & searched when needed • Two flavors (subinterfaces) of List defined • UnorderedList holds elements in any order • Total ordering used with OrderedList
We Don’t Need No Stinking Elements • Note that ADT does not include methods to add • Clear sign more code used to make idea usable • Until we add elements, other methods not usable • But how to store elements in the List? • Store & maintain elements in order within List -or- • Listallow coders to store elements in order they need • Two flavors (subinterfaces) of List defined • UnorderedList holds elements in any order • Total ordering used with OrderedList
List Subinterfaces Defined public interface OrderedList<T> extends List<T> { public void add(T element);} public interface UnorderedList<T> extends List<T> { public void addToFront(T element); public void addToRear(T element); public void addAfter(T elem, T tgt); }
Comparable Interface • Provides simple, abstract way to compare data • Found in java.lang package just like String • Makes coding easier by simplifying search & compare • Total ordering relation needed to implement • Must be exactly one of a< b or a == b or a > b • If a < b&b < cthena < c implied by this definition
Comparable Interface • Single method defined by Comparable<E> intcompareTo(E o) • Compare this instance with o(ther) instance • The only thing that matters is relative value returned • Specific value meaningless & only defines result is: negativeif this<o, zeroif this==opositiveif this>o
Comparable Example class Studentimplements Comparable<Student> {private float qpa;...public intcompareTo(Studentother) { if (qpa > other.qpa) { // Need to return positive number herereturn 73; } else if (qpa== other.qpa) { // Need to return 0 in this case return 0; } else { // Need to return negative number here return -45864;} }
Using Comparable Student match(float grade, Iterable<Student> it){Studentseeker = new Student();seeker.setQPA(grade);for (Student s : it) { if (seeker.compareTo(s) == 0) { return s; } }// Prof. Idiot forgot to define Exception for this unexpected event, so…return null; }
And Your Reaction Should Be… • NHL team class can only have 1 compareTo • But many ways to compare and rank teams
And Your Reaction Should Be… • NHL team class can only have 1 compareTo • But many ways to compare and rank teams • Is writing specific case-by-case code our fate?
Comparator to the Rescue! • Can also use Comparators to compare data • Interface in java.utilpackage (like Scanner) • Unlike Comparable, is separate class doing work • Comparator<E> defines single method intcompare(E o1, E o2) • Specific value meaningless; result defined by: negativeif o1 <o2, zeroif o1 ==o2positiveif o1>o2
Comparator Example class WinComp implements Comparator<NHLTeam>public intcompare(NHLTeamo1,NHLTeamo2) { if (o1.getWins()>o2.getWins()) { return 42; // Need to return positive number here } else if (o1.getWins() == o2.getWins()) { return 0; // Need to return 0 in this case } else { return -68; // Need to return negative number here} } } class PointsComp implements Comparator<NHLTeam>public intcompare(NHLTeamo1,NHLTeamo2) { return (o1.getPoints() - o2.getPoints());} }
Using Comparator intrank(NHLTeamteam, Comparator<NHLTeam> comp,Iterable<NHLTeam> it){int count = 1;for (NHLTeamgoons : it) { if (comp.compare(team, goons) < 0) { count++; } }// Rank of the given team for this metricreturn count; }
Your Turn • Get into your groups and complete activity
For Next Lecture • Before Friday’s lecture read 6.5 – 6.6 • What is ArrayList? How is it used? • How do we implement each list with an array? • What downsides occur when we use an ArrayList? • Week #10 assignment posted to Angel • As usual, will be due next Tuesday