-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,471
  • 17
  • 87
  • 173
pitosalas
  • 9,335
  • 11
  • 59
  • 110

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
  • 277,661
  • 50
  • 283
  • 320
1

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

Smutje
  • 16,951
  • 4
  • 21
  • 38
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