参数中的 3 个点是什么?/什么是可变参数 (...) 参数?参数

2023-09-06 18:18:52 作者:钱包与野区皆空.

I am wondering how the parameter of ... works in Java. For example:

public void method1(boolean... arguments)
{
  //...     
}

Is this like an array? How I should access the parameter?

解决方案 SolidWorks中扫描切除的实体扫描和轮廓扫描有何异同点

Its called Variable arguments or in short var-args, introduced in Java 1.5. The advantage is you can pass any number of arguments while calling the method.

For instance:

public void method1(boolean... arguments) throws Exception {
    for(boolean b: arguments){ // iterate over the var-args to get the arguments.
       System.out.println(b);
    }
 }

The above method can accept all the below method calls.

method1(true);
method1(true, false);
method1(true, false, false);