Presentation is loading. Please wait.

Presentation is loading. Please wait.

The compareTo interface

Similar presentations


Presentation on theme: "The compareTo interface"— Presentation transcript:

1 The compareTo interface
Comparing Strings The compareTo interface

2 Lexicographical Order
Really means, “alphabetical order”, only our alphabet is much larger For Strings, the order of each character is:

3 Comparing Strings Strings are compared index by index until one of the characters is found to be different “testing” vs. “testinG” The first letters that do not match are g and G G comes before g so “testinG” would come first If two strings match for some portion, “Canucks” vs. “Canuck” The empty character always wins, “Canuck” would come first It is usually a good idea to write strings on top of each other when comparing: C O M P A R I S N G First character that does not match: N comes before S, so COMPARING is would come first

4 Lexicographical Rules
Java uses the Unicode character set for its strings. There are 65536, so a convenient summary is: Numbers come before letters Capitals before lower case Lower case are after capitals That’s enough for most applications done by hand

5 Which Comes First? “3456” or “56” “abc” or “123” “Cat” or “Cat woman”
First place they don’t match: index 0 3 comes before 5, therefore, 3456 is less than 56 Remember, this is String comparison not numbers! “abc” or “123” 1 comes before a, therefore, 123 is less than abc “Cat” or “Cat woman” “Cat” is a match with “Cat woman” “Cat woman” still has characters remaining, “Cat” is less than “Cat woman” “aaAA” or “aA” First place they don’t match: index 1 A comes before a, therefore, aA is less than aaAA

6 Primitive Types vs. Objects
You may have noticed so far that we’ve only been comparing, int and double values. In java, the comparison operators only work for primitive types Objects that can be compared will have member functions instead For example, in the String class, all String objects have the methods: compareTo and equals All objects in Java that implement the Comparable interface have the method compareTo

7 The Comparable Interface
The AP subset includes the Comparable<T> interface There is only one method public int compareTo(Object other) Classes you create need to implements Comparable<YOUR_CLASS> public int compareTo(YOUR_CLASS other) implements Comparable Then, in the method definition, (YOUR_CLASS)other; This interface lets you tap into methods in the java library that require comparisons Searching and sorting are the most common

8 Comparing Strings Java can determine lexicographic order by using the compareTo method The compareTo method is not unique to String compareTo has the following requirements returns a positive value when this object is larger than the parameter given returns a negative value when this object is smaller than the parameter given returns zero when this object is equivalent to the parameter given (not necessarily the same object)

9 compareTo “abc”.compareTo(“def”); “def”.compareTo(“abc”);
Returns a negative to indicate that “abc” is less than “def” “def”.compareTo(“abc”); Returns a positive to indicate that “def” is larger than “abc” “abc”.compareTo(“abc”); Returns 0 to indicate that the strings are equal compareTo returns 3 values: An integer less than 0 to indicate the calling string is less than the argument string An integer greater than 0 to indicate the calling string is greater than the argument string 0 to indicate the calling string is equal to the argument string

10 compareTo Consider two Strings: if( one < two )
String one = new String( “one” ); String two = new String( “two” ); if( one < two ) Compares the physical address of one and two Useful in comparing references with == and != Instead you have to say: if( one.compareTo( two ) < 0 )// one < two There is also an equals method to compare two strings

11 compareTo Primitive Types Objects one < two
one.compareTo( two ) < 0 one <= two one.compareTo( two ) <= 0 one > two one.compareTo( two ) > 0 one >= two one.compareTo( two ) >= 0 one == two one.compareTo( two ) == 0 one != two one.compareTo( two ) != 0 Always being compared with 0 Operator stays the same


Download ppt "The compareTo interface"

Similar presentations


Ads by Google