如何使用 ArrayList.addAll()? [英] How to use ArrayList.addAll()?
问题描述
我想用这些字符 +、-、*、^ 等填充 ArrayList
.如何做到这一点而不必用 arrayList.add()添加每个字符代码>?
I want to fill an ArrayList
with these characters +,-,*,^ etc. How can I do this without having to add every character with arrayList.add()
?
推荐答案
Collections.addAll 就是你想要的.
Collections.addAll(myArrayList, '+', '-', '*', '^');
另一种选择是使用 Arrays.asList 像这样:
Another option is to pass the list into the constructor using Arrays.asList like this:
List<Character> myArrayList = new ArrayList<Character>(Arrays.asList('+', '-', '*', '^'));
但是,如果您对固定长度的 arrayList 感到满意,则可以像 list = Arrays.asList(...)
那样简单地进行创建.Arrays.asList 规范指出它返回一个固定长度列表,该列表充当传递数组的桥梁,这可能不是您需要的.
If, however, you are good with the arrayList being fixed-length, you can go with the creation as simple as list = Arrays.asList(...)
. Arrays.asList specification states that it returns a fixed-length list which acts as a bridge to the passed array, which could be not what you need.
这篇关于如何使用 ArrayList.addAll()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!