将嵌套的ArrayList转换为List Java [英] Converting nested ArrayList into List Java
问题描述
我有此方法
public List<List<Stat>> ConvertReportCases (ArrayList<ArrayList<Stat>> stats)
这是一个接口实现,返回类型为List<List<Stat>>
.一般来说,如何将嵌套的ArrayList转换为Java中的List?
It is an interface implementation where the return type is a List<List<Stat>>
. In general how do I convert from a nested ArrayList to List in Java?
谢谢
修改 感谢您的建议,我可以将参数作为List传递,但是由于调用者必须将其作为List传递而将是错误的,而在我的特殊情况下,我可以使用不同的版本(多态性)来检查此问题.
Edit Thanks for the suggestions I could make my parameter passed as List but then it would be wrong since the caller will have to pass it as List, whereas in my particular case I could have different versions (polymorphism) to check this issue.
推荐答案
真的,不需要转换.
如果您无法触摸列表的声明,则应将方法签名更改为此.它使用通配符.
public List<? extends List<Stat>> convertReportCases(List<? extends List<Stat>> stats);
但是,最干净的方法是将方法签名更改为
However, the most clean way would be to change the method signature to
public List<List<Stat>> convertReportCases(List<List<Stat>> stats);
并将列表声明为
List<List<Stat>> list = new ArrayList<List<Stat>>();
for (List<Stat> sublist : list) {
sublist = new ArrayList<Stat>();
}
如果使用List
方法,则可以,不需要任何转换.而且由于ArrayList
是-a List
,并且实际上仅对List
方法有用,所以您应该没事!
If you use List
methods, you will be ok, there will be no converting needed. And since ArrayList
is-a List
and is practically only useful for its List
methods, you should be ok!
这篇关于将嵌套的ArrayList转换为List Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!