问题描述
請問出現java.lang.IllegalArgumentException:Comparisonmethodviolatesitsgeneralcontract!的錯誤訊息查了網路確認是JDK1.7的關係有人說必須更嚴謹的編寫程式碼請問以下的程式碼應該要怎麼改才能避掉這個錯誤呢?謝謝各位的幫忙publicintcompare(Objectoa,Objectob){SortForObjecta=(SortForObject)oa;SortForObjectb=(SortForObject)ob;if(a.getSortValue1()!=b.getSortValue1()){if(Integer.parseInt(a.getSortValue1(),10)-Integer.parseInt(b.getSortValue1(),10)>0){return-1;}else{return1;}}else{if(a.getSortValue2()!=b.getSortValue2()){if(a.getSortValue2().compareTo(b.getSortValue2())>0){return-1;}else{return1;}}else{if(a.getId().compareTo(b.getId())>0){return-1;}else{return1;}}}}
解决方案
解决方案二:
加上相等时的情况即==时返回0
时间: 2024-12-23 05:33:46