我可以很好地比较字符串,但想知道如何对浮点数进行排名?
getChange()返回一个String.我希望能够降序排序.我怎样才能做到这一点?
更新:
package org.stocktwits.helper;
import java.util.Comparator;
import org.stocktwits.model.Quote;
public class ChangeComparator implements Comparator<Quote>
{
public int compare(Quote o1, Quote o2) {
float change1 = Float.valueOf(o1.getChange());
float change2 = Float.valueOf(o2.getChange());
if (change1 < change2) return -1;
if (change1 == change2) return 0; // Fails on NaN however, not sure what you want
if (change2 > change2) return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译时错误:
This method must return a result of type int ChangeComparator.java
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
package org.optimization.geneticAlgorithm;
import org.optimization.geneticAlgorithm.selection.Pair;
public abstract class Chromosome implements Comparable<Chromosome> {
public abstract double fitness();
public abstract Pair<Chromosome> crossover(Chromosome parent);
public abstract void mutation();
public int compareTo(Chromosome o) {
int rv = 0;
if (this.fitness() > o.fitness()) {
rv = -1;
} else if (this.fitness() < o.fitness()) {
rv = 1;
}
return rv;
}
}
Run Code Online (Sandbox Code Playgroud)
每次我运行此代码时,我都会收到此错误:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:835)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:453)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at …Run Code Online (Sandbox Code Playgroud) 如果将错误类型的对象传递给我的compareTo方法,我应该抛出什么类型的Exception?
ClassCastException异常?
给出以下代码:
public abstract class Participant {
private String fullName;
public Participant(String newFullName) {
this.fullName = new String(newFullName);
}
// some more code
}
public class Player extends Participant implements Comparable <Player> {
private int scoredGoals;
public Player(String newFullName, int scored) {
super(newFullName);
this.scoredGoals = scored;
}
public int compareTo (Player otherPlayer) {
Integer _scoredGoals = new Integer(this.scoredGoals);
return _scoredGoals.compareTo(otherPlayer.getPlayerGoals());
}
// more irrelevant code
}
public class Goalkeeper extends Player implements Comparable <Goalkeeper> {
private int missedGoals;
public Goalkeeper(String newFullName) …Run Code Online (Sandbox Code Playgroud) 我有一个类,包含一个Book对象的数组,我需要根据Book的属性(Title或PageNumber)对数组进行排序.问题是我不允许将Comparable类与Book一起使用.您如何推荐我在图书馆中排列图书?写我自己的那种?或者有更简单的方法吗?如果您需要代码片段,请询问!
我正在创建一个实现可比较的自定义类,如果有人试图比较两个我的定义无法比较的对象,我想抛出某种异常.API中是否已存在合适的异常,或者我是否需要创建自己的异常?
我有一个类"Accumulator",它实现了Comparable compareTo方法,我试图将这些对象放入HashSet中.
当我向HashSet添加()时,我在调试器中的compareTo方法中看不到任何活动,无论我在哪里设置断点.另外,当我完成add()时,我在Set中看到了几个重复项.
在这里,我搞砸了什么; 为什么不比较,因此允许欺骗?
谢谢,
IVR复仇者
我正在使用Java JungI图形包和Netbeans 7.我从Java得到以下错误:
Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
Run Code Online (Sandbox Code Playgroud)
以下是与错误相关的代码:
SortedMap<MyVertex, Double> vMap = new TreeMap<MyVertex, Double>();
double curRank = 0;
for(MyVertex v: g.getVertices()) //g is a SparseGraph<MyVertex, MyEdge>
{
curRank = vertexRank.getVertexScore(v);
vMap.put(v, curRank); //**Here is my Error**
}
Run Code Online (Sandbox Code Playgroud)
MyVertex类是我为图表制作的一个类.以下是MyVertex的代码
public class MyVertex
{
int vID; //id for this vertex
double centrality; //centrality measure for this vertex
int degree; //the degree of this vertex
public MyVertex(int id)
{
this.vID …Run Code Online (Sandbox Code Playgroud) 我知道这compareTo会对一个字符串与另一个字符串的相关性返回一个负面或正面的结果,但是为什么:
public class Test {
public static void main(String[] args) {
String y = "ab2";
if(y.compareTo("ac3") == -1) {
System.out.println("Test");
}
}
}
Run Code Online (Sandbox Code Playgroud)
是真的
public class Test {
public static void main(String[] args) {
String y = "ab2";
if(y.compareTo("ab3") == -1) {
System.out.println("Test");
}
}
}
Run Code Online (Sandbox Code Playgroud)
也是如此?
comparable ×10
java ×10
compareto ×2
equals ×2
exception ×2
sorting ×2
arraylist ×1
arrays ×1
casting ×1
comparator ×1
inheritance ×1
openjdk ×1
properties ×1
set ×1
sortedmap ×1
treemap ×1
treeset ×1