【问题标题】:Illegal start of expression and ';' expected error非法开头的表达式和';'预期错误
【发布时间】:2015-08-06 14:53:04
【问题描述】:

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

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

【问题讨论】:

  • 你能在这里复制并粘贴完整的错误信息吗?
  • Collections.sort(); 后面有一个错误的“{”;

标签: java syntax compiler-errors


【解决方案1】:

你把); 放错地方了。它们应该出现在 Comparator&lt;Point&gt;() 的匿名实现之后:

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 应该同样修复。

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2021-11-16
    • 2023-03-26
    • 2014-03-09
    • 1970-01-01
    相关资源
    最近更新 更多