1

I would like to know if there is something like this in Java:

// using JavaScript
let ar = [1,2,3];
Math.max(4, ...ar);
//          ^^^

because I have an EnumSet, and to have a copy of another EnumSet plus another object, i have to do:

var newES = EnumSet.copyOf(someES);
newES.add(p);
doSomething(newES);

Where I would like to do something like:

doSomething(EnumSet.of(p, ...someES.toArray()));
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • Just for clarify: Could you show what the result of `...ar` would be? – akuzminykh Oct 29 '20 at 17:01
  • @akuzminykh sure, is the same of writing `Math.max(4, 1, 2, 3);` – Alberto Sinigaglia Oct 29 '20 at 17:02
  • Hm, but how would you write methods for that? Would you write 100 methods to accept 100 different arities? Passing an array is just more practical. You can iterate over an array. – akuzminykh Oct 29 '20 at 17:07
  • @akuzminykh `EnumSet.of` already accept a arbitrary number of arguments – Alberto Sinigaglia Oct 29 '20 at 17:09
  • 1
    These are related: [Can I pass an array as arguments to a method with variable arguments in Java?](https://stackoverflow.com/q/2925153/12323248), [How to pass an ArrayList to a varargs method parameter?](https://stackoverflow.com/q/9863742/12323248) – akuzminykh Oct 29 '20 at 17:14
  • Also possibly just insert into a list will do. I think that's all that the JavaScript code is doing. I notice in the Java example the OP is using sets, not lists, which might be a problem. – markspace Oct 29 '20 at 17:34

0 Answers0