我一直在寻找一段时间的答案,但实际上我找不到它.
我特别看这个页面.在那里它说该CompareTo方法返回一个整数,指示它是更早,相同还是更晚.我理解它的使用,我明白早期的整数是负的,因为它是0等.
但是这个整数是多少?它是以秒,毫秒,刻度还是根本没有返回差异?我希望你可以帮助我,如果有人能找到这个问题的另一篇文章,请告诉我.老实说,我很惊讶我立即找不到关于这个话题的问题......
我在这里问一个特定的话题 - 我在网上找到了很少有关于此的信息.我正在实现一个F#版本的Minimax算法.我现在遇到的问题是我要比较我的树叶(下面的数据结构).搜索VS给我的错误,我得到了这样的东西:
我曾经拥有的树类型:
type TreeOfPosition =
| LeafP of Position
| BranchP of Position * TreeOfPosition list
Run Code Online (Sandbox Code Playgroud)
和实施IComparable的流行
type staticValue = int
[<CustomEquality;CustomComparison>]
type TreeOfPosition =
| LeafP of Position * staticValue
| BranchP of Position * TreeOfPosition list
override x.Equals(yobj) =
match yobj with
| :? TreeOfPosition as y -> (x = y)
| _ -> false
override x.GetHashCode() = hash (x)
interface System.IComparable with
member x.CompareTo yobj =
match yobj with
| :? TreeOfPosition as y -> …Run Code Online (Sandbox Code Playgroud) 尝试使用Java DelayQueue,我必须实现Delayed需要compareTo() "提供与其getDelay方法一致的排序的方法"的接口..当然,目的是DelayQueue可以轻松地对排队的对象进行排序,使得下一个延迟的对象可以返回给任何接受者.
现在我还需要提前从队列中删除对象.我需要打电话delayQueue.remove(queuedObject).这当然只有在排队的对象具有equals()反映其有效载荷的方法而不是完全不相关的剩余延迟时间时才有效.
因此,compareTo()基于剩余延迟时间而equals()基于排队对象的有效负载,因此它们不一致,正如javadoc中Comparable "强烈推荐"的那样.
问题:我错过了什么,或者这在设计中确实有点怪癖DelayQueue?
我知道这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)
也是如此?
在实现compareTo()时,是否需要考虑"差异"的程度?
例如,如果我有3个对象,C1,C2和C3,那么C1 <C2 <C3.
C1.compareTo(C2)应该返回一个小于C2.compareTo(C3)的整数吗?
Comparable接口的文档似乎没有指定这种或那种方式,所以我猜测程度并不重要,但是知道返回特定数字是否有一些优势会很好(例如,改进TreeSet排序速度或其他东西).
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html#compareTo(T)
如果我有一个类Person实现Comparable(比较personA.height到personB.height,例如),是有可能使用
personA < personB
Run Code Online (Sandbox Code Playgroud)
作为替代品
personA.compareTo(personB) == -1?
Run Code Online (Sandbox Code Playgroud)
这样做有什么问题,还是我需要重载操作符?
我是java的初学者,我试图通过char比较java char中的两个字符串,并通过以下代码查找它们有多少不同的字符,但它不起作用,
min is the min between the 2 strings
for(int i=0; i<min-1; i++){
s1 = w1.substring(j,j++);
s2 = w2.substring(j,j++);
if (! s1.equalsIgnoreCase(s2) ){
counter++;
}
}`
Run Code Online (Sandbox Code Playgroud)
有小费吗?
该行return array[index1].compareTo(array[index2]);提供错误"无法在基本类型double上调用compareTo(double)".如何解决这个问题?
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function implements a comparator of double values :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private class ArrayIndexComparator implements Comparator<Integer>
{
private final double[] array;
public ArrayIndexComparator(double[] array)
{
this.array = array;
}
public Integer[] createIndexArray()
{
Integer[] indexes = new Integer[array.length];
for (int i = 0; i < array.length; i++)
{
indexes[i] = i; // Autoboxing
}
return indexes;
}
@Override
public int compare(Integer index1, Integer index2)
{
// Autounbox from Integer to int to use …Run Code Online (Sandbox Code Playgroud) 在排序列表上工作我得到了一个点,我需要为原始long值实现compareTo()函数.
我不是在寻找明显天真的实现,但是想知道是否有一个优雅的单行代码来做(不创建新的Long(值)).
也许是这样的:
@Override public int compareTo(MyClass that) {
return (int) ((value - that.value) >>> 32);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以验证是否可以工作和/或建议另一个实现?
compareto ×10
java ×8
comparable ×5
equals ×4
c# ×1
char ×1
compare ×1
concurrency ×1
datetime ×1
f# ×1
icomparable ×1
minimax ×1
operators ×1
string ×1
treeset ×1