添加多个项目已经初始化在java中的ArrayList [英] Add multiple items to already initialized arraylist in java

查看:94
本文介绍了添加多个项目已经初始化在java中的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上搜寻它,似乎无法找到语法。我的的ArrayList 可能会被不同的填充根据用户的设置,所以我初始化它

I'm googling it and can't seem to find the syntax. My arraylist might be populated differently based on a user setting, so I've initialized it

ArrayList<Integer> arList = new ArrayList<Integer>();

现在,我想补充的整数几百没有做它一个个 arList.add(55);

推荐答案

如果您有一个包含所有项目另一份清单,你想加你可以做 arList.addAll(otherList)。另外,如果你总是会添加相同的元素列表中,你可以创建一个初始化为包含所有你的价值观和使用的addAll()方法一个新的列表,东西像

If you have another list that contains all the items you would like to add you can do arList.addAll(otherList). Alternatively, if you will always add the same elements to the list you could create a new list that is initialized to contain all your values and use the addAll() method, with something like

Integer[] otherList = new Integer[] {1, 2, 3, 4, 5};
arList.addAll(Arrays.asList(otherList));

或者,如果你不希望创建不必要的数组:

or, if you don't want to create that unnecessary array:

arList.addAll(Arrays.asList(1, 2, 3, 4, 5));

否则,您必须有某种循环单独地添加值到列表中。

Otherwise you will have to have some sort of loop that adds the values to the list individually.

这篇关于添加多个项目已经初始化在java中的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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