Monday, 15 April 2013

java - Implementing compareTo() method in a Generic class -



java - Implementing compareTo() method in a Generic class -

i have project i'm working on , have started with:

public class pair<t extends comparable, e extends comparable> implements comparable{ private e e; private t t; public int compareto(pair arg0) { // todo auto-generated method stub homecoming 0; } }

i need utilize class sort ordered pairs in ascending order. if first ones equal, should sort send point.

could guys please help me start on this?

in class definition, t , e generics lack comparing against themselves. happens pair class. definition of class should be:

public class pair<t extends comparable<t>, e extends comparable<e>> implements comparable<pair<t, e>> { }

now can define how compare pair. here's example:

public class pair<t extends comparable<t>, e extends comparable<e>> implements comparable<pair<t, e>> { private e e; private t t; public int compareto(pair<t, e> pair) { int result = t.compareto(pair.t); homecoming (result == 0) ? e.compareto(pair.e) : result; } }

java generics

No comments:

Post a Comment