0
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 
}

How should I declare the return type for bar?

1
  • 2
    I don't think the line Integer... fIds = bar(ids); is legal. Commented Jun 1, 2011 at 7:57

4 Answers 4

3

You can refer to vararg parameters as an array.

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

See varargs docs

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

To the jvm this is actually an array, and the compiler has hidden the creation of the array.

Sign up to request clarification or add additional context in comments.

Comments

2

Set bar's return type to Integer[] and in foo specify fIds type as Integer[] too.

Comments

1

Variable arguments parameters are just syntactic sugar for arrays, so you can just handle ids as an array of Integer (i.e. an Integer[]).

Comments

1

Something like that:

public Integer[] bar(Integer... ids) {
    List<Integer> res = new ArrayList<Integer>();
    for (Integer id : ids)
        if (shouldBeIncluded(id)) res.add(id);
    return res.toArray();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.