Home > Java > javaTutorial > Java example - Varargs variable parameter usage

Java example - Varargs variable parameter usage

黄舟
Release: 2017-02-16 10:38:42
Original
1859 people have browsed it

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);
   }}
Copy after login

The output result of running the above code is:

数字相加之和为: 55
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template