2012-11-11 88 views
0

我有一個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); 
     } 
    } 
+0

在您的輸出,就3個 「田」 對應LOCATIONNAME,occurenceCount和firstOccurence(按順序)? – assylias

+3

除非我忽略了一些東西,否則''compareTo()'方法看起來應該起作用。通過使用'Integer.compareTo()'可以使它更簡單一些,而不是自己實現對整數的行爲,但這並不妨礙它的正確性。你能給我們多一些代碼嗎,至少我們可以看到'IndexEntry'類是什麼樣的?我也很好奇它中使用了泛型(儘管它可能與你的實際問題無關)。 –

+0

我建議在調試器(或者使用大量的println語句)中使用一個可以重現問題的小輸入來運行代碼。 – DNA

回答

2

你的代碼,因爲你展示它,會產生你給是正確的,而不是你要求它做的不正確輸出的輸出。

下面是一個自包含演示:

https://gist.github.com/4056538

+0

嘿,剛剛添加IndexEntry類,因爲你們有些人問。我仍然在解析你的迴應。 – user1019182

+0

找到了答案,但不能POST它通常: 我不知道爲什麼,但由於某些原因: this.firstOccurrence.equals(other.getFirstOccurrence()) 不等同於: 這一點。 firstOccurrence == other.getFirstOccurrence() 更改該行修正了compareTo方法。由於每一個條目都被實際比較,我誤診了這個問題。對不起,錯誤的信息,並感謝大家的幫助! – user1019182

+2

嗯,那是因爲它是一個Integer而不是int。'=='通過身份比較對象,而不是值,兩個'Integer'類型的變量可以引用具有相同值但仍然不同的對象。儘量避免對數字使用「Integer」;各種奇怪的事情都可能發生。 –