Java Examples - Varargs variable parameters
Java1.5 provides a new feature called varargs is a variable length parameter.
"Varargs" is "variable number of arguments" meant. Sometimes simply referred to as "variable arguments"
Variable number of arguments defined method: just add three consecutive (ie, "..." in the English sentence ellipsis) between a parameter "type" and "parameter name", "." you can make an argument and uncertain match.
The following example creates sumvarargs () method to count the value of all the 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 above code is run output is:
数字相加之和为: 55