1

这个问题涉及泛型并使用 Comparable 和 Comparator 接口。

我有一堂巧克力课:

public class Chocolate implements Comparable<Chocolate> {

    public int compareTo(Chocolate c) {
        if (this.getValue() > c.getValue()) {
            return 1;
        } else if (this.getValue() < c.getValue()) {
            return -1;
        } else {
            return 0;
        }
    }
}

和一个班级对

public abstract class Pair<E extends Comparable<E>, T extends Comparable<T>>
    implements Comparable<Pair<E, T>> { 

    public int compareTo(Pair<E, T> p) {
        if (this.first.compareTo(p.first) > 0) {
            return 1;
        } else if (this.first.compareTo(p.first) < 0) {
            return -1;
        } else {
            if (this.second.compareTo(p.second) > 0) {
                return 1;
            } else if (this.second.compareTo(p.second) < 0) {
                return -1;
            }
        }
        return 0;
    }
}

另一个类 Sorter 有一个 main 方法

public class SortingClient {

    public static void main(String[] args){

        Pair<Chocolate, Chocolate>[] pairs = (Pair<Chocolate, Chocolate>[]) new Pair<?,?>[numPairs];

        Comparator<Pair<Chocolate, Chocolate>> interfaceChocolate = new Comparator<Pair<Chocolate, Chocolate>>(){
            public int compare(Pair<Chocolate,Chocolate> c1, Pair<Chocolate,Chocolate> c2){
                if (c1.compareTo(c2) > 0){
                    return 1;
                }
                else if (c1.compareTo(c2) < 0){
                    return -1;
                }
                else {
                    return 0;
                }
            }
        } * Error here*
    }

我在倒数第二行出现错误,标记为“语法错误,插入“;”以完成 LocalVariableDeclarationStatement” 此错误是什么意思,我该如何解决?

4

4 回答 4

1

即使您正在创建匿名类的实例,也总是需要分号来结束变量声明。

例如,如果你有一个具体的类PairChocolateComparator,你仍然会使用分号:

Comparator<Pair<Chocolate, Chocolate>> interfaceChocolate =
    new PairChocolateComparator<Pair<Chocolate, Chocolate>>();

所以你仍然应该为匿名类实例添加分号:

Comparator<Pair<Chocolate, Chocolate>> interfaceChocolate =
    new Comparator<Pair<Chocolate, Chocolate>>(){
         // body omitted
};
于 2013-06-11T00:03:45.933 回答
0

Ya that is Right look at your CODE you have Not used Semi Colleen to Complete Anonymous Class for exmple you Code

Comparator<Pair<Chocolate, Chocolate>> interfaceChocolate = new Comparator<Pair<Chocolate, Chocolate>>(){
            public int compare(Pair<Chocolate,Chocolate> c1, Pair<Chocolate,Chocolate> c2){
                if (c1.compareTo(c2) > 0){
                    return 1;
                }
                else if (c1.compareTo(c2) < 0){
                    return -1;
                }
                else {
                    return 0;
                }
            }
        } * Error here*

it should be like this.

Comparator<Pair<Chocolate, Chocolate>> interfaceChocolate = new Comparator<Pair<Chocolate, Chocolate>>(){
            public int compare(Pair<Chocolate,Chocolate> c1, Pair<Chocolate,Chocolate> c2){
                if (c1.compareTo(c2) > 0){
                    return 1;
                }
                else if (c1.compareTo(c2) < 0){
                    return -1;
                }
                else {
                    return 0;
                }
            }
        }; * Error here*

look at the ; that will finish Class

Hope this will help.

于 2013-11-13T08:04:39.237 回答
0

有时会出现此错误(即使您在变量声明的末尾有分号),因为您在此声明的正下方有一个特殊字符(例如 *),这会导致上一行出现类似这样的一般错误。至少要寻找的东西……嗯,a,替我向你的 motha 打个招呼,嗯

于 2015-01-12T00:55:37.117 回答
0

只需按照消息提示添加分号即可。这就是赋值语句(to interfaceChocolate)的完成。

于 2013-06-11T00:01:17.503 回答