非法表达的开始和';'预期的错误

时间:2015-05-25 07:14:02

标签: java syntax compiler-errors

我得到了非法的表达开始和;预期的错误。我搜索了类似的问题,但我无法解决我的问题。

public int compare(Point point1, Point point2)

这是完整的方法。

public static void sortByX(List<? extends Point> points)
{
    Collections.sort(points, new Comparator<Point>() );
    {
        public int compare(Point point1, Point point2)
        {
            if (point1.x < point2.x)
                return -1;
            if (point1.x > point2.x)
                return 1;
            return 0;
        }
    }
}

public static void sortByY(List<? extends Point> points)
{
    Collections.sort(points, new Comparator<Point>() );
    {
        public int compare(Point point1, Point point2)
        {
            if (point1.y < point2.y)
                return -1;
            if (point1.y > point2.y)
                return 1;
            return 0;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

你有);错误的地方。它们应该出现在Comparator<Point>()的匿名实现之后:

public static void sortByX(List<? extends Point> points)
{
    Collections.sort(points, 
        new Comparator<Point>() //); - remove these
        {
            public int compare(Point point1, Point point2)
            {
                if (point1.x < point2.x)
                    return -1;
                if (point1.x > point2.x)
                    return 1;
                return 0;
            }
        }); // add ); here
}

sortByY应该同样修复。

相关问题