java - 我应该如何声明可变参数参数

标签 java variadic-functions

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 的返回类型?

最佳答案

您可以将 vararg 参数作为数组引用。

public Integer[] bar(Integer... ids) {
..
}

参见varargs docs

It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process

对于jvm来说这实际上是一个数组,编译器隐藏了数组的创建。

关于java - 我应该如何声明可变参数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6197895/

相关文章:

c++ - 如何创建可变参数模板函数来移动参数值并处理左值和右值?

c++ - 使用省略号的回退函数 : can we force the size of the parameters pack?

java - 索引越界异常 : cant figure out what is happening? 学校作业

java - 决定在运行时加载哪个 JavaConfig 类

c++ - 将可变参数函数参数转发到另一个可变参数函数而无需成本

c++ - 单个函数的两个可变参数模板?

java - 帮助在java中解析JSON

java - 如何使用注解映射Set集合的simplevalue类型

java - 即使在使用迭代器进行迭代时也获取 ConcurrentModifcationException

java - 优雅地等同于 Java 中 C 的 vsscanf()