如何使用ArrayList.addAll()? [英] How to use ArrayList.addAll()?

查看:178
本文介绍了如何使用ArrayList.addAll()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这些字符+,-,*,^等填充 ArrayList 。如何做到这一点而不必用<$ c $添加每个字符c> 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屋!

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