简体   繁体   中英

Relay variable number of arguments from one method to another with variable number of arguments

Say I have a public function a() and a private method b() . Both have variable number of parameters. a() needs to pass its parameters to a private function b() , which then converts the variable number of parameters to an EnumSet and passes it to c() .

public void a(MyEnum ... enums){
    // what code do I need here, to pass the parameters to b()?
}
private void b(MyEnum ... enums){
    for (int i = 0; i < enums.length; i++){
        // make EnumSet and pass it to c()
    }
}
private void c(EnumSet<MyEnum> enumSet){
}

How can I code a() ?

b(enums);

同样,您可以一起跳过b并将for循环移到方法a

An arbitrary number of arguments (called varargs ) is a means to not manually create an array. Inside such a method it is an array.

And you can pass such an array as an argument to a vararg method:

public void a(MyEnum ... enums) {
    b(enums);
}
private void b(MyEnum ... enums) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM