2012-06-19 26 views
0

我在創建一個包含'Vertex3 <T>'的實例的Set。我在與行的問題一點點創建的集:在java中模板cals的設置

public Set<Vertex3<Integer>> verticies = new Set<Vertex3<Integer>>(); 

Eclipse是強調在紅色的「設置」部分等號的右邊,並顯示錯誤消息「不能實例化type Set < Vertex3 <Integer> >「。

'Vertex3 <Ť>' 被定義爲如下:

public class Vertex3 <T> { 
    public T x; 
    public T y; 
    public T z; 

    public Vertex3() { 
     // do nothing 
    } 

    public Vertex3(T x, T y, T z) { 
     this.x = x; 
     this.y = y; 
     this.z = z; 
    } 

    public Vertex3(T x, T y) { 
     this.x = x; 
     this.y = y; 
    } 
} 

可以提供理解任何幫助。

+0

所有的答案都是正確的 - 你可能想從你的類型判斷一個HashSet。 –

回答

1

Set是一個接口,這就是爲什麼你不能實例化它。你必須實例化一個具體類型,像HashSet的(或TreeSet中,或LinkedHashSet):

public Set<Vertex3<Integer>> verticies = new HashSet<Vertex3<Integer>>(); 

的HashSet和LinkedHashSet店獨特的價值觀...... HashSet的表現優於LinkedHashSet,你可能想使用HashSet的。

0

Set是一個接口。你不能實例化它。用HashSet或TreeSet替換右邊的部分。