Java spread operator

前端 未结 6 900
死守一世寂寞
死守一世寂寞 2021-01-01 10:11

I am not sure of the vocabulary I am using here, please correct me if I\'m wrong.

In Javascript, I had the following code:

let args = [1,2,3];

func         


        
6条回答
  •  情歌与酒
    2021-01-01 10:19

    Unfortunately, you can't do this. The spread operator works in javascript because functions are allowed to accept two few (left out arguments are undefined) or too many arguments (extra arguments are ignored) of any type. Java, being strongly and statically typed, must always know exactly how many and what kind of arguments you are passing before you even compile the code.

    You can probably find a hackaround with Java 8's functional interfaces, method references and var-args, but it would require so much boilerplate that I won't even bother posting it here.

提交回复
热议问题