on it road.com
向数组列表中添加多个项
要将另一个集合中的所有项目添加到 arraylist,请使用 ArrayList.addAll() 方法。
public class ArrayListExample
{
public static void main(String[] args)
{
//List 1
List<String> namesList = Arrays.asList( "alex", "BobRobert", "Lucie");
//List 2
ArrayList<String> otherList = new ArrayList<>();
//Copy all items from list 1 to list 2
otherList.addAll(namesList);
System.out.println(otherList);
}
}
程序输出。
[alex, BobRobert, Lucie]
请注意,此方法复制列表中的元素引用。
所以这两个列表都引用了相同的对象。
如果我们改变一个列表中的一个对象,另一个列表中的相同对象也将被改变。
日期:2020-09-17 00:09:29 来源:oir作者:oir
