集合大家用的最多的封装好的数据结构;那么在创建好一个集合,如何将自己的数据导入呢?
- 方法一:
static <T> boolean addAll(Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection.
Collections 接口中有这么一个静态方法,可以将一批数据导入自己的集合
你可以这样:
List list = new ArrayList();
boolean b = Collections.addAll(list, 1, 2, 3);
将数字1,2,3添加到集合list中;
你也可以这样:
Integer [] arr = {4, 2, 3, 12};
//int [] arr = {4, 2, 3, 12};
List list = new ArrayList();
boolean b = Collections.addAll(list,arr);
将数组的数字添加到集合中,当然大家都知道可变长参列表就是数组这里是不是又说废话了:其实就是想让 大家注意一下添加数组时要用对象数组不要用基本类型数组;
- 接下来还有一种方法:那就是数组先变成集合然后第哦啊用个自己和的addAll()方法;
- 第一步数组变集合:
List<Integer> integers = Arrays.asList(1, 2, 3, 4);
- 第二步添加:
List list = new ArrayList();
list.addAll(integers);