简体   繁体   中英

adding an unknown number of parameters to a method call in Java

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.

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

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM