java 自定義型別比較大小

2021-05-26 08:35:33 字數 701 閱讀 3448

如果是自己寫了乙個class 作資料型別需要比較大小則需implements comparable 介面, 並在class 中重寫compareto()方法,例如:

public class a implements comparable

private double score;

/**

* @return the score

*/ public double getscore()

@override

public int compareto(a o)

else if(result <1)

return  compare;

}然後在其他地方,就可以直接比較型別為a的物件大小或者排序:

/**

* @param args

*/ public static void main(string args)

}在其他程式裡用最大堆:

maxheapcomparator = new maxheapcomparator();

priorityqueuemaxheap = new priorityqueue(20, maxheapcomparator);

或者從大到小排序:

int testarray= ;

arrays.sort(testarray, maxheapcomparator);

IOS float型別比較大小

int bellowgreenlow 0,bellowgreenhigh 0,bellowyellowhigh 0,aboveyellowhigh 0 int countsum 0 兩個floatleixing 比較時,不能直接相減,當比較是否相等時,應該用兩者之差的絕對值 與精度 相比,如果小於則...

String 型別比較大小的方式

第一種直接用字串類的compareto方法 1 2 3 string t1 20131011 string t2 20131030 intresult t1.compareto t2 第二種 適用於整數string型別 是把這個日期字串轉換成long 1 2 3 4 dateformat sdf n...

自定義型別

typedef型別 typedef 已有型別 新建型別 示例 typedef double area,volume typedef int number number i1 area a enum enum 列舉型別名 enum week 預設sun 0,可以比較 如果修改必須形如enum week...