繁体   English   中英

将多个ArrayList对象的所有元素添加到单个ArrayList对象中

[英]Add all the elements of multiple ArrayList objects into a single ArrayList object

就我而言,我有4个ArrayList对象,如下所示:

ArrayList<MyProduct> lstStyle;
ArrayList<MyProduct> 2ndStyle;
ArrayList<MyProduct> 3rdStyle;
ArrayList<MyProduct> 4thStyle;

我想将每个ArrayList中的所有元素添加到名为Style的新ArrayList中

ArrayList<MyProduct> Style;

我可以在不循环每个ArrayList的情况下执行此操作吗?

使用List#addAll(Collection c)方法

Style.addAll(lstStyle);
Style.addAll(2ndStyle);
Style.addAll(3rdStyle);
Style.addAll(4thStyle);

当然,您需要首先实例化所有列表,否则将面临NullPointerException

Collections.addAll(Style, lstStyle, 2ndStyle, 3rdStyle, 34thStyle);

使用addAll方法

ArrayList<MyProduct> Style;

 Style.addAll(lstStyle);
 Style.addAll(2ndStyle);
 Style.addAll(3rdStyle);
 Style.addAll(4thStyle);  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM