与通配符相关的泛型 [英] generics with context to wild cards
本文介绍了与通配符相关的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
从您的描述中猜测,
列表与LT;字符串> firstList = new ArrayList< String>();
public< T>列表与LT; T> createSecondList(List< T> firstList)
{
return new ArrayList< T>();
}
In an interview with regard to generics the interviewer ask a question that there are two list,one list is of String type (that is one arraylist is of string type) and we add some strings in it, now create the second array list which will be of type the the first list you created, means the second ararylist will accept the objects type of first list created he give me the hint that it could be achieve through generics , please advise how to achieve this.
解决方案
At a guess from your description,
List<String> firstList = new ArrayList<String>();
public <T> List<T> createSecondList(List<T> firstList)
{
return new ArrayList<T>();
}
这篇关于与通配符相关的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文