我想定义一个实现通用Comparable接口的类.在我的课堂上,我还定义了一个泛型类型元素T.为了实现接口,我将比较委托给T.这是我的代码:
public class Item<T extends Comparable<T>> implements Comparable<Item> {
private int s;
private T t;
public T getT() {
return t;
}
@Override
public int compareTo(Item o) {
return getT().compareTo(o.getT());
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我收到以下错误信息:
Item.java:11: error: method compareTo in interface Comparable<T#2> cannot be applied to given types;
return getT().compareTo(o.getT());
^
required: T#1
found: Comparable
reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Comparable<T#1> declared in class …Run Code Online (Sandbox Code Playgroud) 我正在将一些代码升级到Java 5,我显然不了解泛型.我有其他类实现Comparable一次,我已经能够实现了.但是现在我有一个类,由于继承,最终尝试为2种类型实现Comparable.这是我的情况:
我有以下类/接口:
interface Foo extends Comparable<Foo>
interface Bar extends Comparable<Bar>
abstract class BarDescription implements Bar
class FooBar extends BarDescription implements Foo
Run Code Online (Sandbox Code Playgroud)
有了这个,我得到错误'接口Comparable不能用不同的参数实现多次......'
为什么我不能在FooBar中实现compareTo(Foo foo),还有在BarDescription中实现的compareTo(Bar)?这不是简单的方法重载?
编辑:我有很多扩展BarDescription的类.如果我在Bar上删除Comparable的类型参数,使其保持原始状态,那么在排序扩展BarDescription的所有类时,我会得到一堆编译器警告.这可以通过下面的通配符解答来解决吗?这个答案看起来非常复杂,难以理解维护.
我正在尝试编写一个需要两个Comparables 的泛型max函数.
到目前为止我有
public static <T extends Comparable<?>> T max(T a, T b) {
if (a == null) {
if (b == null) return a;
else return b;
}
if (b == null)
return a;
return a.compareTo(b) > 0 ? a : b;
}
Run Code Online (Sandbox Code Playgroud)
这无法编译
The method compareTo(capture#5-of ?) in the type Comparable<capture#5-of ?> is not applicable for the arguments (T)
Run Code Online (Sandbox Code Playgroud)
我认为这是说,?in Comparable<?>可以被解释为参数a的一种类型,而参数b的另一种类型,因此它们无法进行比较.
我如何从这个洞中挖掘自己?
我想有一个compareTo方法,它接受一个Real(一个用于处理任意大而精确的实数的类[好吧,只要它的长度小于2 ^ 31])和compareTo方法一个对象,但Java不让我,我没有足够的经验知道为什么.
我只是尝试修改类来实现Comparable,我在下面收到了这些错误消息.我真的不明白错误信息是什么意思,但我知道它与可怕的方式有关,我试图给我们的课程一些灵活性,为我制作的每一种方法提供所有不同的方法签名,我可以修复它通过删除compareTo(Object other)方法,但我最好保留它.所以我真正要问的是:有没有办法让这些错误消息消失而不删除compareTo(Object other)方法,这些错误究竟是什么意思?
另外,我知道已经有一些像BigInteger这样的内置Java类,以及我正在尝试使用这个类的东西,但我正在为了与Project Euler(https://一起使用)的乐趣/满足而这样做projecteuler.net/).
Jake@Jake-PC /cygdrive/c/Users/Jake/Documents/Java/Mathematics
$ javac Real.java
Real.java:377: error: name clash: compareTo(Real) in Real overrides a method whose erasure is the same as another method, yet neither overrides the other
public int compareTo(Real other)
^
first method: compareTo(Object) in Real
second method: compareTo(T) in Comparable
where T is a type-variable:
T extends Object declared in interface Comparable
Real.java:440: error: name clash: compareTo(Object) in Real and compareTo(T) in Comparable have the …Run Code Online (Sandbox Code Playgroud) 如何使用"<"或">"将Java中的两个对象进行比较,例如
MyObject<String> obj1= new MyObject<String>(“blablabla”, 25);
MyObject<String> obj2= new MyObject<String>(“nannaanana”, 17);
if (obj1 > obj2)
do something.
Run Code Online (Sandbox Code Playgroud)
我把MyObject类标题设为
public class MyObject<T extends Comparable<T>> implements Comparable<MyObject<T>>
并且创建了方法Comp但是我获得的所有增益现在我可以在对象列表上使用"sort",但是如何直接比较两个对象?是
if(obj1.compareTo(obj2) > 0)
do something
Run Code Online (Sandbox Code Playgroud)
唯一的办法?
以下给出了一条错误消息:
public static List<Comparable<?>> merge(Set<List<Comparable<?>>> lists) {
List<Comparable<?>> result = new LinkedList<Comparable<?>>();
HashBiMap<List<Comparable<?>>, Integer> location = HashBiMap.create();
int totalSize;
for (List<Comparable<?>> l : lists) {
location.put(l, 0);
totalSize += l.size();
}
boolean first;
List<Comparable<?>> lowest; //the list with the lowest item to add
int index;
while (result.size() < totalSize) {
first = true;
for (List<Comparable<?>> l : lists) {
if (! l.isEmpty()) {
if (first) {
lowest = l;
}
else if (l.get(location.get(l)).compareTo(lowest.get(location.get(lowest))) <= 0) { //error here
lowest = …Run Code Online (Sandbox Code Playgroud) 我知道我们可以按照我们的要求对存储在Collection中的对象进行排序或排序.
虽然我得到了深刻的理解,但我不相信安排的升序和降序是通过(a - b) - >升序或(b - a) - >降序来实现的,其中"a"和"b"是等级我们选择比较的成员.
例:
public int compareTo(Student s) {
return this.grade - s.grade; //ascending order
// return s.grade - this.grade; // descending order
}
Run Code Online (Sandbox Code Playgroud)
订购对象元素背后的逻辑是什么?怎么"(this.grade - s.grade)"如果正1移动"this.grade"前面并按顺序放"s.grade",为什么不以其他方式?谁验证了比较结果(+ 1,-1,0),然后分别按升序或降序排列,是否有任何文档描述了这部分的内部工作?
public class Student implements Comparable <Student>{
String name;
int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public int compareTo(Student s) {
return this.grade - s.grade; //ascending order
// return s.grade - this.grade; // descending order
}
public String …Run Code Online (Sandbox Code Playgroud) 我可以很好地比较字符串,但想知道如何对浮点数进行排名?
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异常?
comparable ×10
java ×10
generics ×5
interface ×2
sorting ×2
arraylist ×1
collections ×1
comparator ×1
erasure ×1
exception ×1
object ×1
openjdk ×1
overriding ×1
raw-types ×1