什么VB6方法允许两个相同类型的自定义对象(在类模块中定义)相互比较?我认为这与Java的compareTo方法相当,但我无法在任何地方找到它.
我想知道如何使用DateTime方法比较.NET中的两个DateTime对象Compare,CompareTo或者Equals不比较刻度.
我只需要毫秒或秒的容差级别.
如何才能做到这一点?
请考虑以下代码:
namespace ConsoleApplication1 {
class Program
{
static void Main(string[] args)
{
Console.WriteLine(100.CompareTo(200)); // prints -1
Console.WriteLine(((decimal)100).CompareTo((decimal)200)); // prints -1
Console.WriteLine(((short)100).CompareTo((short)200)); // prints -100
Console.WriteLine(((float)100).CompareTo((float)200)); // prints -1
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,Int16上的CompareTo方法返回除-1,0和1以外的值有什么特殊原因吗?
ILSpy显示它以这种方式实现
public int CompareTo(short value)
{
return (int)(this - value);
}
Run Code Online (Sandbox Code Playgroud)
而这种方法是以这种方式在Int32上实现的
public int CompareTo(int value)
{
if (this < value)
{
return -1;
}
if (this > value)
{
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在Comparable一个包含单个int成员的普通类上实现接口.
我可以这样实现它:
@Override
public int compareTo ( final MyType o )
{
return
Integer.valueOf( this.intVal ).compareTo(
Integer.valueOf( o.intVal )
);
}
Run Code Online (Sandbox Code Playgroud)
但是这(可能)会创建2个完全不必要的Integer对象.
或者我可以从Integer类中尝试一下真正的剪切和粘贴方法:
@Override
public int compareTo ( final MyType o )
{
int thisVal = this.intValue;
int anotherVal = o.intValue;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
Run Code Online (Sandbox Code Playgroud)
这非常有效,但不需要复制代码.
是否有一个库可以实现这个缺失Integer(和Double和Float)方法?
public static int compare ( int v1, int v2 );
Run Code Online (Sandbox Code Playgroud) 我试图比较object[]单个类型的对象(在运行时未知).它们是System.string,int,decimal,Datetime,或bool类型.
有没有办法比较其中两个对象,以确定一个对象是否大于或小于另一个,而不必先将它们转换为适当的类型?
class Test{
public static void main(String[] args){
int a = 1;
int b = 5;
Integer c = new Integer(1);
Integer d = 5; //autoboxing at work
System.out.println(c.compareTo(d));
System.out.println(a.compareTo(b));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不a.compareTo(b)编译(int cannot be dereferenced)?我知道compareTo需要的对象,但我认为自动装箱会自动作出int的Integer必要时.为什么在这种情况下不会发生自动装箱?还有什么其他情况不会发生?
我现在正在学习基础Java,并且我的代码存在问题,我无法弄清楚.这基本上就是标题所说的.我的Java编译器告诉我,我的自定义compareTo方法有一个错误,说它需要返回一个int.问题是,据我所知,它返回一个int.但它仍然给我一个错误.有人可以在我的代码中指出什么是错的?而且我已经在课堂上实现了Comparable.这是我的方法:
public int compareTo(Homework other) {
if (getDaysLate() < other.getDaysLate()) {
return -1;
} else if ((dateSubmitted == other.dateSubmitted)
&& (files.compareTo(other.files) == -1)) {
return -1;
} else if ((dateSubmitted == other.dateSubmitted)
&& (files == other.files)) {
if (name.compareTo(other.name) == -1) {
return -1;
} else if (name.compareTo(other.name) == 1) {
return 1;
} else if (name.compareTo(other.name) == 0) {
return 0;
}
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用的containsKey方法TreeMap,但是以某种方式我遇到了问题。
存储在树形图中的对象的定义与equals()不会产生相同的结果compareTo()。这是有意的。
但是,的文档java.util.Map说:
如果此映射包含指定键的映射,则返回true。更正式地说,当且仅当此映射包含键k的映射(例如)时,才返回true
(key==null ? k==null : key.equals(k))。(最多可以有一个这样的映射。)
因此,我尝试了以下操作:
c = someModifiedObject();
boolean t1 = sm.containsKey(c);
someObject c2 = new someObject();
boolean t2 = sm.containsKey(c2);
boolean t3 = c.equals(new Chain());
int t4 = c.compareTo(new Chain());
Run Code Online (Sandbox Code Playgroud)
t1为true,因为对象位于树形图中。t3为true,因为t1等于t3(对于更改equals()运算符而言)t4为false,但是,t2也为false。似乎TreeMap使用compareTo()而不是equals()确定对象是否存在。
是否有排序映射的另一种实现,可以equals()用来检查对象是否存在?
所以我正在使用 Compare-Object,它对于比较文件效果很好。但如果只是字符串呢?有没有办法找出字符串之间的差异?CompareTo() 擅长报告存在差异,但不报告差异是什么。例如:
PS:> $a = "PowerShell rocks"
PS:> $b = "Powershell rocks"
PS:> $a.CompareTo($b)
1
PS:> Compare-Object -ReferenceObject $a -DifferenceObject $b
PS:>
Run Code Online (Sandbox Code Playgroud)
什么也没有返回。
有什么方法可以让我了解字符串之间的实际差异,而不仅仅是存在差异?
我正在用java编写电话簿程序,我需要按字母顺序列出列表中的人员,为此我需要为java中的列表编写排序算法,它应该只使用compareTo()方法.所以有人可以帮助我做到这一点吗?
public void listAlpha()
{
Node tempNode = head;
for(int i = 0; i <= size; i++)
{
for(int j = 0; j <= i; j++)
{
int comparison = ((tempNode.getNext().getElement().getName()).compareTo(tempNode.getElement().getName()));
if(comparison < 0)
{
Person tempPerson = tempNode.getElement();
tempNode.setElement(tempNode.getNext().getElement());
tempNode.getNext().setElement(tempPerson);
tempNode = tempNode.getNext();
}
}
}
Run Code Online (Sandbox Code Playgroud)
(顺便说一下,这是一个家庭作业,我使用自己的数据结构.)
这是我上面写的方法所属的类:
import java.util.*;
/** Singly linked list implementation .*/
public class SLinkedList<E> implements LinkedList<E>, Iterable<E> {
protected Node<E> head; // head node of the list
protected Node<E> tail; // tail node of …Run Code Online (Sandbox Code Playgroud)