43

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"});
Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
transient_loop
  • 5,984
  • 15
  • 58
  • 117

3 Answers3

74

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.

Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
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");
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
9

You can try

List<String> list = Arrays.asList(new String[] {"a","b","c"});
user219882
  • 15,274
  • 23
  • 93
  • 138
  • 2
    No, you can do `Arrays.asList("a", "b", "c");`, which is shorter, and more similar to the OP's original question. – Tim Pote May 23 '12 at 20:26
  • 6
    What Thomas said is still valid and doesn't deserve a downvote. – Jeshurun May 23 '12 at 20:26
  • No, what he said is "You *have* to write", which is untrue, so, yes, it *does* deserve a downvote. – Tim Pote May 23 '12 at 20:27
  • 1
    @TimPote OMG. What can I say ... I changed it. But you don't have to be so nit-picky – user219882 May 23 '12 at 20:29
  • 2
    @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. – Tim Pote May 23 '12 at 20:30
  • @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. – Marko Topolnik May 23 '12 at 20:48
  • @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. – Tim Pote May 23 '12 at 20:53
  • 1
    @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... – Marko Topolnik May 23 '12 at 21:01