2

I have a method that I want to expand (rather than writing a new method which does basically the same thing), by adding an unknown number of parameters to the end of the list of parameters.

If I do this, will I have to change all the calls to the method? I guess the question is, does the unknown parameter include the case there being no parameter passed in at all?

For instance, if I have a method:

queryFactory(int [] typeArgs, int queryType, int[] ... args){}

Could I call:

queryFactory(typeArgsInstce, queryTypeInstce)

And then when I need to add parameters to the query call:

queryFactory(typeArgsInstce, queryTypeInstce, argsInstce)

Where argsInstce is an array of integers containing extra arguments.

I would like to just edit this method rather than writing a new one which does almost the exact same thing except it has some arguments to add to queries. I will simply write another method if by editing this one I will have to change every other call to this method.

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
  • 3
    (have you already tried it?) – Andreas Dolk Nov 20 '12 at 16:22
  • Look at my answer [here](http://stackoverflow.com/a/12534579/646634) for info on var args. – Brian Nov 20 '12 at 16:22
  • 4
    Note: you probably want `queryFactory(int [] typeArgs, int queryType, int... args){}` – Puce Nov 20 '12 at 16:24
  • 1
    Be aware, by specifying `int[] ... args`, this is exactly the same as specifying `int[][] args`. The `...` already turns it into an array, so by specifying `[]` in your type along with the `...`, you're creating a 2-dimensional array, and I doubt this is what you want, as others here have indicated. – Brian Nov 20 '12 at 16:31

3 Answers3

6
public static void main(String[] args) {
    method(1);      // <- compile error
    method(1,2);
    method(1,2,3);
    method(1,2,3,4);
}

private static void method(int i1, int i2, int...i3) {
    // do something
}

So to answer the question in words: we need 2 arguments at minimum. This passes an empty array ´i3[]´ to the method. Arguments number 3 and above are treated as array values.


It makes no difference...

public static void main(String[] args) {
    method(new int[]{1});      // <- compile error
    method(new int[]{1},2);
    method(new int[]{1},2,new int[]{3,4});
    method(new int[]{1},2,new int[]{3,4},new int[]{5,6});
}

private static void method(int[] i1, int i2, int[]...i3) {
    // do something
}

The varargs parameter has to be the last so it won't conflict with the first array

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
2

As you asked Could I call: you can call here is the example

public static void main(String[] args) {

        int[] i = { 1, 2, 3 };
        int[] i1 ={1,1,1,1};
        System.out.println("Sum: " + sum(i,2,i1));
        System.out.println("Sum: " + sum(i,2));
    }

    static int sum(int[] numbers1,int num,int[]... numbers2) {
        int t[][] = numbers2;
        int total = 0;
        for (int i = 0; i < t.length; i++) {
            for (int j = 0; j < t[i].length; j++) {
                System.out.print(t[i][j]);
                total += t[i][j];
            }

        }
        for(int test : numbers1)
             total+=test;

        total+=num;

        return total;
    }
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • You don't have to assign `numbers2` into a new reference like that, you can use it directly, i.e. `numbers2[i][j]`, but still, a good reference point with some actual code, +1. – Brian Nov 20 '12 at 17:01
0

I understand that you don't want to change the signature of your method because you will need to change every call of that method, so you could create a method that have the 3 args with all the code, and overload the same method with only 2 args, but in this method you only call the method with 3 args, the last arg will be null. I know is not that you want, but you wouldn't repeat the code and change the signature of the method.

public void queryFactory(int [] typeArgs, int queryType, int... args){
     // do something
}

public void queryFactory(int [] typeArgs, int queryType){
    queryFactory(typeArgs,queryType,null);
}
ChuyAMS
  • 480
  • 2
  • 9
  • This would force him to do a `null`-check in the 3-argument option and is completely unnecessary. He can call `queryFactory` with only two parameters, and it will create `args` as an empty array, so he doesn't have to even do a `null`-check, since `args.length` will be zero. – Brian Nov 20 '12 at 17:02