我有一個IndexList的ArrayList,它是一個帶有字符串的簡單類,以及兩個整數和下面顯示的compareTo()
方法。我的問題是,當我在ArrayList上調用Collections.sort()
時,大多數情況下所有的排序都是正確的,但有時候兩個元素永遠不會進行比較,導致排序錯誤。Java Collections.sort()沒有比較元素
一些供參考代碼:
//Get a list of the values
ArrayList<IndexEntry<T>> cleanEntries = new ArrayList<IndexEntry<T>>(matches.get(query).values());
//Sort Files/URLS
Collections.sort(cleanEntries);
public int compareTo(IndexEntry<T> other) {
if(this.occurrenceCount < other.getOccurrenceCount()){
return 1;
}
else if(this.occurrenceCount > other.getOccurrenceCount()){
return -1;
}
else{//Need to compare Initial Positions
if(this.firstOccurrence == other.getFirstOccurrence()){
return this.getLocationName().compareTo(other.getLocationName());
}
else if(this.firstOccurrence > other.getFirstOccurrence()){
return 1;
}
else{
return -1;
}
}
}
這裏是所有IndexEntry的字母「C」的一些輸出例如:如何發生次數和初次發生後,被確定爲
c
"index.html", 6, 2
"chickadee.html", 3, 2
"cuckoo.html", 3, 2
"crow.html", 3, 2
"crane.html", 3, 2
"cardinal.html", 3, 2
"quail.html", 2, 2
"robin.html", 2, 2
"finch.html", 2, 2
"kingfisher.html", 2, 2
"swan.html", 2, 2
"turkey.html", 2, 2
"ibis.html", 2, 2
"falcon.html", 2, 2
"albatross.html", 2, 2
"tern.html", 2, 2
"dove.html", 2, 2
"mockingbird.html", 2, 2
"owl.html", 2, 2
"sparrow.html", 2, 2
"hawk.html", 2, 2
"magpie.html", 2, 2
"nighthawk.html", 2, 2
"gull.html", 2, 2
"bluebird.html", 2, 2
"raven.html", 2, 2
"blackbird.html", 2, 2
"sandpiper.html", 2, 2
"pheasant.html", 2, 2
"roadrunner.html", 2, 2
"duck.html", 2, 2
"vulture.html", 2, 2
"pigeon.html", 2, 2
"heron.html", 2, 2
"stork.html", 2, 2
"warbler.html", 2, 2
"woodpecker.html", 2, 2
"hummingbird.html", 2, 2
"mallard.html", 2, 2
"osprey.html", 2, 2
"swallow.html", 2, 2
"wren.html", 2, 2
"goose.html", 2, 2
"meadowlark.html", 2, 2
"pelican.html", 2, 2
"starling.html", 2, 2
"loon.html", 2, 2
"eagle.html", 2, 2
"puffin.html", 2, 2
"egret.html", 2, 2
公告是一樣的,它停止排序。正確的輸出是:
c
"index.html", 6, 2
"cardinal.html", 3, 2
"chickadee.html", 3, 2
"crane.html", 3, 2
"crow.html", 3, 2
"cuckoo.html", 3, 2
"albatross.html", 2, 2
"blackbird.html", 2, 2
"bluebird.html", 2, 2
"dove.html", 2, 2
"duck.html", 2, 2
"eagle.html", 2, 2
"egret.html", 2, 2
"falcon.html", 2, 2
"finch.html", 2, 2
"goose.html", 2, 2
"gull.html", 2, 2
"hawk.html", 2, 2
"heron.html", 2, 2
"hummingbird.html", 2, 2
"ibis.html", 2, 2
"kingfisher.html", 2, 2
"loon.html", 2, 2
"magpie.html", 2, 2
"mallard.html", 2, 2
"meadowlark.html", 2, 2
"mockingbird.html", 2, 2
"nighthawk.html", 2, 2
"osprey.html", 2, 2
"owl.html", 2, 2
"pelican.html", 2, 2
"pheasant.html", 2, 2
"pigeon.html", 2, 2
"puffin.html", 2, 2
"quail.html", 2, 2
"raven.html", 2, 2
"roadrunner.html", 2, 2
"robin.html", 2, 2
"sandpiper.html", 2, 2
"sparrow.html", 2, 2
"starling.html", 2, 2
"stork.html", 2, 2
"swallow.html", 2, 2
"swan.html", 2, 2
"tern.html", 2, 2
"turkey.html", 2, 2
"vulture.html", 2, 2
"warbler.html", 2, 2
"woodpecker.html", 2, 2
"wren.html", 2, 2
不管怎麼說,就像我說的我都省略了大量的信息,但如果有的話,想到我會很感激的想法。事實上,在創建ArrayList之後,「匹配」數據結構不會被修改。編輯: 嘿,我還在閱讀你的迴應。非常感謝您的快速幫助!這裏是整個IndexEntry類,因爲你們幾個問:
class IndexEntry<extends Comparable<T>> implements Comparable<IndexEntry<T>>{
private T theLocation;
private Integer occurrenceCount;
private Integer firstOccurrence;
public IndexEntry(T location, Integer firstOcc, Integer occCount){
theLocation = location;
occurrenceCount = occCount;
firstOccurrence = firstOcc;
}
public int compareTo(IndexEntry<T> other) {
if(this.occurrenceCount < other.getOccurrenceCount()){
return 1;
}
else if(this.occurrenceCount > other.getOccurrenceCount()){
return -1;
}
else{//Need to compare Initial Positions
if(this.firstOccurrence == other.getFirstOccurrence()){
return this.getLocationName().compareTo(other.getLocationName());
}
else if(this.firstOccurrence > other.getFirstOccurrence()){
return 1;
}
else{
return -1;
}
}
}
public String getLocationName(){
if(theLocation instanceof File){
return ((File) theLocation).getAbsolutePath();
}
else{
return theLocation.toString();
}
}
public Integer getFirstOccurrence(){
return firstOccurrence;
}
public Integer getOccurrenceCount(){
return this.occurrenceCount;
}
public void addToOccurrenceCount(Integer toAdd){
occurrenceCount += toAdd;
}
public void setFirstOccurrence(Integer firstOcc){
firstOccurrence = new Integer(firstOcc);
}
}
在您的輸出,就3個 「田」 對應LOCATIONNAME,occurenceCount和firstOccurence(按順序)? – assylias
除非我忽略了一些東西,否則''compareTo()'方法看起來應該起作用。通過使用'Integer.compareTo()'可以使它更簡單一些,而不是自己實現對整數的行爲,但這並不妨礙它的正確性。你能給我們多一些代碼嗎,至少我們可以看到'IndexEntry'類是什麼樣的?我也很好奇它中使用了泛型(儘管它可能與你的實際問題無關)。 –
我建議在調試器(或者使用大量的println語句)中使用一個可以重現問題的小輸入來運行代碼。 – DNA