2014-02-24 29 views
1

進行參數作爲Java中的初學者,我得到以下編譯錯誤嘗試自定義比較分配給一組:設置比較器不是通用的;它不能與參數

The type mycode.pointComparator is not generic; it cannot be parameterized with arguments

這裏是我的代碼:

Set<Point3d> cornerPoints = new TreeSet<Point3d>(new pointComparator<Point3d>()); 

class pointComparator implements Comparator<Point3d> { 

     @Override 
     public int compare(Point3d o1, Point3d o2) { 
      if(!o1.equals(o2)){ 
       return -1; 
      } 
      return 0; 
     } 
    } 

我進口Java.util.

更新: 刪除此錯誤<Point3d>參數結果:

No enclosing instance of type mycode is accessible. Must qualify the allocation with an enclosing instance of type mycode (e.g. x.new A() where x is an instance of mycode).

+2

讓我們來看看...'類型mycode.pointComparator不是通用的;它不能用參數'參數化。只有一種方法來閱讀此。 –

+2

從'new point'中刪除''比較者()'... – assylias

回答

2

這工作:

public static void main(String[] args) throws Exception { 
    // Look at the following line: diamond operator and new. 
    Set<Point> points = new TreeSet<>(new PointComparator()); 
} 

static class PointComparator implements Comparator<Point> { 

    @Override 
    public int compare(Point p1, Point p2) { 
     if (!p1.equals(p2)) { 
      return -1; 
     } 
     return 0; 
    } 
} 
+0

謝謝!該類應該是靜態的。 – Amirali

相關問題