public void foo(Integer... ids) {
Integer... fIds = bar(ids);
}
public void bar(Integer... ids) {
// I would like to eliminate few ids and return a subset. How should I declare the return argument
}
我该如何声明bar的返回类型?我该如何声明一个可变参数
public void foo(Integer... ids) {
Integer... fIds = bar(ids);
}
public void bar(Integer... ids) {
// I would like to eliminate few ids and return a subset. How should I declare the return argument
}
我该如何声明bar的返回类型?我该如何声明一个可变参数
您可以参考可变参数作为数组。
public Integer[] bar(Integer... ids) {
..
}
它仍然是真实的多参数必须在阵列中已过,但可变参数功能自动化和隐藏进程
到JVM这实际上是一个数组,编译器隐藏了数组的创建。
将bar
的返回类型设置为Integer[]
并且foo
将fIds
类型指定为Integer[]
。
变量参数参数只是数组的语法糖,因此您可以将ids
作为Integer
(即Integer[]
)的数组来处理。
类似的东西:
public Integer[] bar(Integer... ids) {
List<Integer> res = new ArrayList<Integer>();
for (Integer id : ids)
if (shouldBeIncluded(id)) res.add(id);
return res.toArray();
}
我不认为行'整数... FIDS =杆(IDS);'是合法的。 – trutheality 2011-06-01 07:57:49