0

hi I have a method using varargs

public void methodA(int... is);

And I'd like to put a List into it like this

List<Integer> listOfIds = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
methodA(listOfIds);

how can I do ? ps: I tried this Using Arrays.stream(listOfIds).boxed().Collector.toArray( Integer[]::new ); but java told me Cannot resolve method 'stream(java.util.List<java.lang.Integer>)'

lolozen
  • 386
  • 1
  • 4
  • 19
  • There is no such thing as `List` in Java. – Andreas Jan 03 '20 at 19:22
  • Specifically you should use the answer from the duplicate that utilizes Java 8 streams AKA `methodA(listOfIds.stream().mapToInt(i -> i).toArray())` which converts the `List` into `int[]` instead of `Integer[]`. – Nexevis Jan 03 '20 at 19:24
  • `listOfIds` is a `List`, so why are you trying to treat it as an array by calling `Arrays.stream()`? You're doing it the wrong way. Use `listOfIds.stream().mapToInt(i->i).toArray()` instead, as shown in the duplicate answer. – Andreas Jan 03 '20 at 19:26

0 Answers0