how to use addall() method of collections?
i need to use it to merge开发者_如何学C two ordered list of objects.
From the API:
addAll(Collection<? extends E> c)
: Adds all of the elements in the specified collection to this collection (optional operation).
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]"
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);
Related questions
- Arrays.asList() not working as it should?
Collection all = new HashList();
all.addAll(list1);
all.addAll(list2);
I m coding some android, and i found this to be extremely short and handy:
card1 = (ImageView)findViewById(R.id.card1);
card2 = (ImageView)findViewById(R.id.card2);
card3 = (ImageView)findViewById(R.id.card3);
card4 = (ImageView)findViewById(R.id.card4);
card5 = (ImageView)findViewById(R.id.card5);
card_list = new ArrayList<>();
card_list.addAll(Arrays.asList(card1,card2,card3,card4,card5));
Versus this standard way i used to employ:
card1 = (ImageView)findViewById(R.id.card1);
card2 = (ImageView)findViewById(R.id.card2);
card3 = (ImageView)findViewById(R.id.card3);
card4 = (ImageView)findViewById(R.id.card4);
card5 = (ImageView)findViewById(R.id.card5);
card_list = new ArrayList<>();
card_list.add(card1) ;
card_list.add(card2) ;
card_list.add(card3) ;
card_list.add(card4) ;
card_list.add(card5) ;
精彩评论