-2

I feel like this two line program should be expressible in one line but I can't get the {} array literal to work inside the asList call. Is there a way?

String[] a = {"Whiskey", "Tango", "Foxtrot"};
myList.addAll(Arrays.asList(a));
halfer
  • 19,824
  • 17
  • 99
  • 186
pitosalas
  • 10,286
  • 12
  • 72
  • 120

3 Answers3

5

Arrays.asList receives an ellipsis (T...), so you just don't need the array literal:

myList.addAll(Arrays.asList("Whiskey", "Tango", "Foxtrot"));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

myList.addAll(Arrays.asList("Whiskey", "Tango", "Foxtrot"));

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

As I don't think you need string array here, you can use like that

List<String> x = new ArrayList<String>() {{add("Whiskey");add("Tango")add("Foxtrot");}};
user3662273
  • 544
  • 4
  • 10