Initialize List<> with Arrays.asList [duplicate]
Asked Answered
S

3

44

Why does this work:

String[] array = {"a", "b", "c"};
List<String> list = Arrays.asList(array);

but this does not:

List<String> list = Arrays.asList({"a","b","c"});
Scad answered 23/5, 2012 at 20:24 Comment(0)
B
75

This is a short hand only available when constructing and assigning an array.

String[] array = {"a", "b", "c"};

You can do this though:

List<String> list = Arrays.asList("a","b","c");

As asList can take "vararg" arguments.

Bartram answered 23/5, 2012 at 20:25 Comment(2)
The second code example doesn't compile in Java 8. It says "cannot convert List to List<String>"Ohm
That's no true, works fine in Java 5 to at least 11, just tested all. You've done some mistake @BlueRaja-DannyPflughoeft , maybe wrong import, java.awt.List or similar?Bartram
D
13

Your question is why one works and the other does not, right?

Well, the reason is that {"a","b","c"} is not a valid Java expression, and therefore the compiler cannot accept it.

What you seem to imply with it is that you want to pass an array initializer without providing a full array creation expression (JLS 15.10).

The correct array creation expressions are, as others have pointed out:

String[] array = {"a", "b", "c"};

As stated in JLS 10.6 Array Initializers, or

String[] array = new String[]{"a", "b", "c"};

As stated in JLS 15.10 Array Creation Expressions.

This second one is useful for inlining, so you could pass it instead of an array variable directly.

Since the asList method in Arrays uses variable arguments, and variable arguments expressions are mapped to arrays, you could either pass an inline array as in:

List<String> list = Arrays.asList(new String[]{"a", "b", "c"});

Or simply pass the variable arguments that will be automatically mapped to an array:

List<String> list = Arrays.asList("a","b","c");
Deforce answered 23/5, 2012 at 20:44 Comment(0)
M
9

You can try

List<String> list = Arrays.asList(new String[] {"a","b","c"});
Membranophone answered 23/5, 2012 at 20:25 Comment(8)
No, you can do Arrays.asList("a", "b", "c");, which is shorter, and more similar to the OP's original question.Murrhine
What Thomas said is still valid and doesn't deserve a downvote.Childers
No, what he said is "You have to write", which is untrue, so, yes, it does deserve a downvote.Murrhine
@TimPote OMG. What can I say ... I changed it. But you don't have to be so nit-pickyMembranophone
@Tomas Sorry, not trying to be nit-picky, but I don't want the OP to get the wrong understanding. There is another, shorter alternative to this solution. If nobody had said otherwise, he might have believed you.Murrhine
@TimPote A downvote is still a too aggressive measure. Why didn't you just leave the comment for 10 minutes and see if Tomas corrects himself, and if he didn't, then downvote? That's what a cooperative mind would do on SO.Fruitful
@MarkoTopolnik Perhaps, you're right. Generally, I downvote anything that's patently false in some way (which this was, albeit a minor way). Some users would have responded to a comment, but I've come across many that don't. Just so you know, I took away my downvote as soon as he made his revision. I don't know who still has one outstanding.Murrhine
@TimPote Agreed, many don't respond, and I figured the outstanding DV wasn't yours. Also, a very short answer somehow invites a DV if not absolutely correct. Some kind of gut reaction at work there, I would guess...Fruitful

© 2022 - 2024 — McMap. All rights reserved.