创建涉及泛型和接口的ArrayLists

时间:2017-03-26 07:14:54

标签: java generics arraylist interface

所以我有一个通用接口,它有两个参数,所以我在另一个类中实现了Type,我在其中创建了一个构造函数。在我的主类中,我试图创建一个对象的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 个答案:

答案 0 :(得分:1)

这有帮助吗?

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