Return type vararg?

You can do,

Grok[] generateMoreGroks() {

If a method takes a varargs parameter, it is the same as taking an array.

Now you need to overload foo to allow it to take some Grok instances, and the rest as varargs,

foo(int x, Grok... rest)
foo(int x, Grok g1, Grok... rest)
foo(int x, Grok g1, Grok g2, Grok... rest)
foo(int x, Grok g1, Grok g2, Grok g3, Grok... rest)

Where foo methods are like,

foo(int x, Grok g1, Grok... rest) {
     Grok[] groks = new Grok[rest.length + 1];
     groks[0] = g1;
     System.arrayCopy(rest, 0, groks, 1, rest.length);
     foo(x, groks);
}

This is a bit ugly.


No there isn't... but change the function signature and that will work just fine.

public Grok[] generateMoreGroks() {
    return new Grok[] {
        new Grok(), new Grok(), new Grok() };
}

Tags:

Java