i need to use it to merge two ordered list of objects.
Asked
Active
Viewed 5.0k times
9
-
1What do you mean by merge? What are you trying to do? – polygenelubricants Jun 16 '10 at 10:53
3 Answers
20
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

Community
- 1
- 1

polygenelubricants
- 376,812
- 128
- 561
- 623
-
There are libraries that provide `int[] <-> Integer[]` conversion, or you can use Guava's `Ints.asList(int...)` with `addAll` to a `List
`, etc. I can elaborate on these points if this is what OP wants to do. – polygenelubricants Jun 16 '10 at 11:05 -
This is telling me for List
: The type List is not generic; it cannot be parameterized with arguments – fIwJlxSzApHEZIl Oct 22 '14 at 03:04
3
Collection all = new HashList();
all.addAll(list1);
all.addAll(list2);

Sjoerd
- 74,049
- 16
- 131
- 175
-
-
1@bhavna: Not according to the signature in the API docs, no: http://java.sun.com/javase/6/docs/api/java/util/Collection.html#addAll(java.util.Collection) – T.J. Crowder Jun 16 '10 at 10:50
1
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) ;

ERJAN
- 23,696
- 23
- 72
- 146
-
I would write: "card_list.add(card1 = (ImageView)findViewById(R.id.card1));" if I really need the variables and not just the list. – The incredible Jan Jul 04 '17 at 10:43