compareto

Rules to implement compare method

我们两清 提交于 2019-12-05 21:41:58
like compareTo, that have to be "reflexive, antisymmetric and transitive", are there any rules to implement the compare method?? thanks PermGenError From Comparator API : The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.) The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0. Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn

Implementing custom compareTo

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 23:26:24
问题 @Override public int compareTo(Object t) { if(t instanceof Student) { Student s = (Student)t; return (this.name.compareTo(s.name)); } else return -1; } This is my compareTo method implementation for comparing two Student objects based on their name. Is it possible to compare two such objects based on multiple fields i.e., both name and age? 回答1: Yes it is possible to compare two objects based on different sort sequences using Comparator interface compare method. You need to create a sort

Using the compareTo method with an array to sort students by name and test score

你离开我真会死。 提交于 2019-12-04 19:02:58
I need to use the comparable interfact and a compareTo method to sort a list of students alphabetically and then by test score. I'm struggling at getting this to work in my application. The list of names needs to be read from a text file. I don't know how many names will be in the text file that my professor uses except that it will be less than 100. I am also supposed to display the average of the grades at the bottom as well as write a message next to any student that is 15 points below average. I have not really gotten to the writing message part as I am currently stuck on getting the names

Internal working of the Sort() and CompareTo() methods

倾然丶 夕夏残阳落幕 提交于 2019-12-04 04:31:57
问题 I've been trying to figure out how the CompareTo() method works internally and I failed. I've searched this site and read some posts, and I think I've seen all there is to see in MSDN about this subject and I just don't seem to get it. An MSDN example: public int CompareTo(object obj) { if (obj == null) { return 1; } Temperature otherTemperature = obj as Temperature; if (otherTemperature != null) { return this.temperatureC.CompareTo(otherTemperature.temperatureC); } else { throw new

How to write hashCode method for a particular class?

て烟熏妆下的殇ゞ 提交于 2019-12-03 08:46:30
问题 I'm trying to generate a hashCode() method for my simple class but i'm not getting anywhere with it. I would appreciate any help. I've implemented the equals() method, which looks as follows, and would also like to know if I need to implement compareTo() method. I've imported java.lang.Character to use character.hashCode() but it doesn't seem to work. private class Coord{ private char row; private char col; public Coord(char x, char y){ row = x; col = y; } public Coord(){}; public char getX()

How to write hashCode method for a particular class?

梦想与她 提交于 2019-12-02 22:46:35
I'm trying to generate a hashCode() method for my simple class but i'm not getting anywhere with it. I would appreciate any help. I've implemented the equals() method, which looks as follows, and would also like to know if I need to implement compareTo() method. I've imported java.lang.Character to use character.hashCode() but it doesn't seem to work. private class Coord{ private char row; private char col; public Coord(char x, char y){ row = x; col = y; } public Coord(){}; public char getX(){ return row; } public char getY(){ return col; } public boolean equals(Object copy){ if(copy == null){

Collections.sort() error

倖福魔咒の 提交于 2019-12-02 14:08:25
问题 I am trying to sort a list of type A named BinOrder in class B according to Class A's int r. However i am receiving this error for the line Collections.sort(BinOrder); The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<A>) Class A: public class A{ int s; int r; public A(int si, int ri) { s=si; r= ri; } } Class B: import java.util.ArrayList; import java.util.Collections; public class B implements Comparable<A> { public Iterator<A> randomMethodName

LString class, using linked lists to make strings, java

馋奶兔 提交于 2019-12-02 08:39:01
问题 I'm having trouble writing a compareTo() and charAt() methods for a linked list object that builds strings. The class, called LString contains a constructor and a few other methods. It runs with another file that tests its ability as a linked list string builder, and I am receiving this error message: Running constructor, length, toString tests (10 tests) Starting tests: .......... Time: 0.000 OK! (10 tests passed.) Running compareTo and equals tests (18 tests) Starting tests: EEEEEEEE.EEE.E.

Collections.sort() error

三世轮回 提交于 2019-12-02 07:21:21
I am trying to sort a list of type A named BinOrder in class B according to Class A's int r. However i am receiving this error for the line Collections.sort(BinOrder); The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<A>) Class A: public class A{ int s; int r; public A(int si, int ri) { s=si; r= ri; } } Class B: import java.util.ArrayList; import java.util.Collections; public class B implements Comparable<A> { public Iterator<A> randomMethodName(int a) { ArrayList<A> BinOrder = new ArrayList<A>(); A a = new A(1,3) A a2 = new A(1,4) BinOrder.add(a);

LString class, using linked lists to make strings, java

☆樱花仙子☆ 提交于 2019-12-02 07:20:18
I'm having trouble writing a compareTo() and charAt() methods for a linked list object that builds strings. The class, called LString contains a constructor and a few other methods. It runs with another file that tests its ability as a linked list string builder, and I am receiving this error message: Running constructor, length, toString tests (10 tests) Starting tests: .......... Time: 0.000 OK! (10 tests passed.) Running compareTo and equals tests (18 tests) Starting tests: EEEEEEEE.EEE.E.... Time: 0.016 There were 12 failures: 1) t21aTestCompareTo[0](LStringTest$LStringCompareToTest) java