【发布时间】:2020-06-03 13:54:26
【问题描述】:
我有以下代码存根,我正在从属性文件中读取一组值。 仅当它们不为 NULL 时,我才需要使用这些值将其作为参数传递给函数。
public static void main(String[] args) {
String arg1 = "arg1";
String arg2 = "arg2";
String arg3 = null;
String arg4 = "arg4";
.
.
.
testMethod(arg1, arg2, arg3);
}
public void testMethod(String... values) {
}
在上面的代码中sn-p。我想用参数 arg1、arg2、arg4 调用 testMethod(),只是因为 arg3 为 NULL。
参数的数量可能会有所不同。不会一直是 4。
我的代码应该动态检查参数是否不为 NULL 并将其传递给 testMethod()。
我可以用 Java 实现吗?如果是,有人可以帮助我..
【问题讨论】:
-
您可以做的是通过传递所有变量的数组来调用
testMethod(String... values)方法,在该方法中您可以手动检查变量是否为null以及您想要的其他方法打电话。