2017-03-26 59 views
-1

所以我有一個泛型接口有兩個參數,所以我在另一個類中創建了一個構造函數。在我的主類中,我試圖創建一個對象的arrayList,它將使用我在其中實現泛型接口的類中創建的構造函數。我只需要幫助爲這個過程設置arrayList。任何幫助將不勝感激。 這是接口:創建ArrayList涉及泛型和接口

public interface Trip<K, T> { 
    public K getPerson(); 
    public T getLocation(); 
} 

這是實現:

public class TripImpl <K,T> implements Trip <K,T> { 
    private K person; 
    private T location; 
    private Date dateOfVisit; 
    public TripImpl(K person, T locations, Date dateOfVisits){ 
     person = persons; 
     location = locations; 
     dateOfVisit = dateOfVisits; 
    } 
//theres more code with the getters but this is just to show the constructor 
} 

主要類:

public class Collect { 
    ArrayList <TripImpl> tlist = new ArrayList<TripImpl>(); 
     String persons, locatione; 
     Date visitd; 
     for (int i =0; i<uPerson.length; i++){ 
      persons = uPerson[i]; 
      locatione = uLocation[i]; 
      visitd = uDate[i]; 
      TripImpl u<String, String> = new TripImpl<String, String>(persons, locatione, visitd); 
      ulist.add(u); 
     } 
} 

這是給我一個錯誤的主類的第一行。它說TripImpl是一種原始類型,並且參考通用類型TripImpl應該參數化

+1

請顯示驗證碼。 – ravi

+0

並解釋你卡在哪裏。 – shmosel

+0

對不起,我編輯它包括代碼和卡在哪裏 – Plumorane

回答

1

這有幫助嗎?

VisitImpl<String, String> u = new VisitImpl<String, String>(persons, locatione, visitd); 
+0

是的!這有助於解決我在嘗試創建新的Trip Implement時遇到的錯誤。但是現在我的第一行有一個問題:ArrayList tlist = new ArrayList (); 錯誤說「TripImpl是一個原始類型,並且引用應該被參數化」 – Plumorane

+1

@Plumorane然後這樣做:'ArrayList >' – shmosel

+0

很高興爲您提供幫助。如果遇到更多麻煩,最好打開另一個問題 –