如何使用集合的addall()方法? [英] how to use addall() method of collections?

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

问题描述

我需要用它来合并两个有序的对象列表。

i need to use it to merge two ordered list of objects.

推荐答案

来自API:


addAll(Collection<?extends E> c) :添加指定的所有元素集合到这个集合(可选操作)。

addAll(Collection<? extends E> c): Adds all of the elements in the specified collection to this collection (optional operation).

这是一个使用 List 的例子,这是一个有序的集合:

Here's an example using List, which is an ordered collection:

    List<Integer> nums1 = Arrays.asList(1,2,-1);
    List<Integer> nums2 = Arrays.asList(4,5,6);

    List<Integer> allNums = new ArrayList<Integer>();
    allNums.addAll(nums1);
    allNums.addAll(nums2);
    System.out.println(allNums);
    // prints "[1, 2, -1, 4, 5, 6]"



< hr>

On int [] vs Integer []



int 可自动执行到整数时, int [] 不是autoboxable到 Integer []


On int[] vs Integer[]

While int is autoboxable to Integer, an int[] is NOT "autoboxable" to Integer[].

因此,您将获得以下行为:

Thus, you get the following behaviors:

    List<Integer> nums = Arrays.asList(1,2,3);
    int[] arr = { 1, 2, 3 };
    List<int[]> arrs = Arrays.asList(arr);



相关问题




  • Arrays.asList()无法正常工作?

  • Related questions

    • Arrays.asList() not working as it should?
    • 这篇关于如何使用集合的addall()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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