与通配符相关的泛型 [英] generics with context to wild cards

查看:127
本文介绍了与通配符相关的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个关于泛型的采访中,访问者询问一个问题,即有两个列表,一个列表是String类型的(即一个ArrayList是字符串类型的),我们在其中添加一些字符串,现在创建第二个数组列表将是您创建的第一个列表类型,意味着第二个ararylist将接受创建的第一个列表的对象类型,他给我的提示可以通过泛型实现,请告知如何实现此目的。

解决方案

从您的描述中猜测,

 列表与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆