Consolidating Java (vi)----variable-parameter methods in Java (very practical OH)

Source: Internet
Author: User

Java provides a method of variable parameters, that is, the number of parameters of a method can be indeterminate, defined with a "...".

Import Java.util.arraylist;import Java.util.list;public class Variableparameter {//To find the maximum value in several integers public int getmax (int). ..       Items) {//define mutable parameters items int max = Integer.min_value;    The number of times is the minimum value that int can represent, with a value of -2147483648 for (int item:items) {max = item > Max Item:max;    Take a large value} return max;    }//returns the number of numbers larger than the public int getlagernum (int number,int ... items) {//If there are other parameters besides variable length parameters, the variable length parameter must be placed at the last int sum = 0;    for (int item:items) {if (item > number) {sum++;    }} return sum; } public static void Main (string[] args) {variableparameter VP = new Variableparameter ();  SYSTEM.OUT.PRINTLN ("Maximum Value:" +vp.getmax ());  Can not write parameter System.out.println ("Maximum Value:" +vp.getmax (2,1,4,7,2,-1,3,3));        Several parameters can be written directly in the parameters System.out.println ("Maximum:" +vp.getmax ( -1,4,5,0,1));  Several parameters can be written directly in the parameters System.out.println ("Number of large Numbers:" +vp.getlagernum (9, 1,11,5,14,-1,9,21));        9 is number, followed by items int a[] = new int[] {1,11,5,14,-1,9,21}; System.out.println ("More than nUmber number of large numbers: "+vp.getlagernum (9,a)); Items can also be substituted with an array}}
The results of the operation are as follows:

Maximum value: 2147483648 Maximum: 7 Maximum: 5 The number of large numbers: 3 is the number of large numbers: 3

As you can see from the above code:

1. After defining a mutable parameter in a method, we can manipulate the parameter as if it were an array of operations;

2. If the method has other parameters besides the variable parameters, the variable parameter must be put to the last;

3. When you call a method that uses a mutable parameter:

A. Can not write parameters, that is, the incoming null parameter;

B. Parameters can be written directly inside, separated by commas between parameters;

C. An array can be passed in;


Keep looking down:

public class Variableparameter {public    void comp (int ... items) {    System.out.println ("1");    }        public void Comp (int item1,int item2) {    System.out.println ("2");    }    public static void Main (string[] args) {variableparameter VP = new Variableparameter (); Vp.comp (1, 2);}}
Operation Result:

2

From this we can see:

4. Methods that have variable parameters can be overloaded, and when called, a method that takes precedence over the length of a parameter is preferred if it can be matched to a parameter's fixed length.


Go on:

public class Variableparameter {public    void comp (int ... items) {    System.out.println ("1");    }        public void Comp (int[] items) {      //Will error: Duplicate method Comp (int[]) in type Variableparameter    System.out.println ("2");    }    public static void Main (string[] args) {}}

When attempting to use an array as a parameter to implement the overload, an error occurs stating that the mutable parameter conflicts with the array, but:

public class Variableparameter {public    void comp (int[] items) {      //Will error: Duplicate method Comp (int[]) in type Variab Leparameter    System.out.println ("2");    }    public static void Main (string[] args) {variableparameter VP = new Variableparameter (); Vp.comp (1,2,3,4);    The method Comp (int[]) in the type Variableparameter are not applicable for the arguments (int, int, int, int)}}
If you define a method that is an array of parameters, calling it like a method called a mutable parameter will give an error, stating that the mutable parameter is not an array, so we can summarize:

5: Variable parameters can be compatible with array parameters, but array parameters cannot be compatible with mutable parameters.







Consolidating Java (vi)----variable-parameter methods in Java (very practical OH)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.