[英]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. 其中
argsInstce
是包含额外参数的整数数组。
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.
这会将空数组“ i3 []”传递给该方法。 Arguments number 3 and above are treated as array values.
大于等于3的参数被视为数组值。
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 varargs参数必须是最后一个,因此它不会与第一个数组冲突
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. 我知道您不想更改方法的签名,因为您将需要更改该方法的每次调用,因此您可以创建一个在所有代码中都包含3个arg的方法,并仅重载同一个方法2个参数,但是在此方法中,您仅使用3个参数调用该方法,最后一个arg将为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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.