比較器中的返回值實際上是什麼意思?比較器的返回值
例如:
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t=new TreeSet(new MyComparator());
t.add(new Integer(20));
t.add(new Integer(10));
t.add(new Integer(30));
t.add(new Integer(100));
System.out.println(t);
}
class MyComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
return 0;
}
}
}
如果返回類型爲1,則其實際上返回
[20,10,30,100]
如果返回類型是-1,則其實際返回
[100,30,10,20]
如果返回類型爲0,那麼它實際上返回
[20]
請告訴我這說明什麼?
通常「0」表示「==」,「1」表示「>」,「-1」表示「<」 –
「1」,「-1」和「0」不是類型,而是值。 –
比較器的功能在其文檔中有很好的闡述。嘗試閱讀一點之前,只是問一個問題。 – Dunes