0

我有一个 IndexEntry 的 ArrayList,它是一个简单的类,带有一个字符串、两个整数和一个compareTo()如下所示的方法。我的问题是,当我Collections.sort()大多数时候调用 ArrayList 时,一切都会正确排序,但有时永远不会比较两个元素,从而导致列表排序错误。

一些代码供参考:

//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;
        }
    }
}   

以下是字母“C”的所有 IndexEntry 的一些示例输出:

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);
        }
    }
4

1 回答 1

2

正如您所展示的那样,您的代码会产生您提供的正确输出,而不是您声称的错误输出。

这是一个独立的演示:

https://gist.github.com/4056538

于 2012-11-11T22:33:33.453 回答