Home >Java >javaTutorial >Java example - Varargs variable parameter usage
Java1.5 provides a new feature called varargs, which is a variable-length parameter.
"Varargs" means "variable number of arguments". Sometimes it is simply called "variable arguments"
The method of defining a variable number of actual parameters: as long as it is between the "type" and "parameter name" of a formal parameter Adding three consecutive "." (that is, "...", the ellipsis in the sentence in English) can match it with an indefinite number of actual parameters.
The following example creates the sumvarargs() method to count the values of all numbers:
/* author by w3cschool.cc Main.java */public class Main { static int sumvarargs(int... intArrays){ int sum, i; sum=0; for(i=0; i< intArrays.length; i++) { sum += intArrays[i]; } return(sum); } public static void main(String args[]){ int sum=0; sum = sumvarargs(new int[]{10,12,33}); System.out.println("数字相加之和为: " + sum); }}
The output result of running the above code is:
数字相加之和为: 55
The above is the Java example - Varargs variable The content of parameter usage, please pay attention to the PHP Chinese website (www.php.cn) for more related content!