lec18 - CSE 12 Ordering Objects Comparison and ordering...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
18 Comparison and ordering Comparing primitive types and objects The Comparable interface Designing a generic sorting method Making a class Comparable CSE 12 Ordering Objects
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Comparing Objects for Ordering Every Java object “knows how” to compare itself to another object for equality : call its equals() method, passing the other object as argument But some algorithms (e.g. sorting algorithms) and some data structures (e.g. heaps, binary search trees) require that objects be compared to each other for ordering How to do that?. ..
Background image of page 2
Comparing Objects for Ordering In Java, the <= , >= , == , != operators can be used to compare primitive type values Of these, only == and != can be used to compare object references. .. and they just compare the address stored in the reference, and do not compare for ordering To compare objects for ordering in Java, understand and use the Comparable<T> interface
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Comparable<T> Interface public interface Comparable<T>{ int compareTo( T o ); } obj1.compareTo(obj2) returns a negative integer if obj1 is “less than” obj2 a positive integer if obj1 is “greater than” obj2 zero if obj1 is “equal to” obj2 Test Using compareTo() to Compare Objects Equivalent Relational Operation to Compare Primitives a.compareTo( b ) < 0 a < b a.compareTo( b ) > 0 a > b a.compareTo( b ) == 0 a == b a.compareTo( b ) != 0 a != b a.compareTo( b ) <= 0 a <= b a.compareTo( b ) >= 0 a >= b
Background image of page 4
Classes implement Comparable // The String class declares that it implements // the Comparable interface so that an instance of // itself can be compared to another String . public class String implements Comparable<String> Note the use of a specific type in the type parameter field. This guarantees that we can only compare Strings to Strings
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Generic Methods In Java, classes can be generic, and so can methods Declaration Application
Background image of page 6
Towards a Generic Sorting Method Suppose we want to write a sorting method that is as generic as possible: that is, one that can take as argument any array of things that can be compared for ordering. What should the type of the parameter be?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 17

lec18 - CSE 12 Ordering Objects Comparison and ordering...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online