0

有人知道如何在日志中进行排序吗?我试过使用compareToIgnoreCase,但我只知道如何将它与字符串一起使用。

public static void main(String[] args)
    {
        StringLogInterface log;
        log = new ArrayStringLog("Example Use");
        log.insert("Elvis");
        log.insert("King Louis XII");
        log.insert("Captain Kirk");
        System.out.println(log);
        System.out.println("The size of the log is " + log.size());
        System.out.println("Elvis is in the log: " + log.contains("Elvis"));
        System.out.println("Santa is in the log: " + log.contains("Santa"));
    }
4

1 回答 1

1

我假设您的代码是:http ://cs.nyu.edu/~gottlieb/courses/ds/bookFiles/ch02/stringLogs/ArrayStringLog.java

首先将 getLog 方法添加到 ArrayStringLog 类:

public String[] getLog() {
    return log;
}

然后排序功能是:

Arrays.sort(log.getLog(), new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {
        return o1.compareToIgnoreCase(o2);
    }
});
于 2013-09-22T21:10:13.440 回答