"Java Learning Note 27" method for Java8 multiple parameters

Source: Internet
Author: User

When a parameter is passed in Java, it is followed by the use of "..." after the type:

public static void Main (string[] args) {
Teststringargs ();//no parameter passed in
Teststringargs ("one");//a parameter passed in
Teststringargs ("One", "two", "three"),//3 string parameter passed in
Teststringargs (New string[]{"One", "two", "three"});//You can see that passing in three String arguments and passing in an array of length 3 results in the same example
Teststringargs (New string[]{"One", "one", "one", "three"},new string[]{"one", "one", "three"});//This will prompt an error.

Testintegerargs ();
Testintegerargs (1);
Testintegerargs (a);
Testintegerargs (New integer[]{1,2,3});
}

//somewhat similar to () and (String s1,string s2 ...) and (string[] s) 3 combined functions.
public static void Teststringargs (String ... s) {
if (s.length==0) {
System.out.println ("0 parameters passed in");
}else if (s.length==1) {
System.out.println ("1 parameters passed in");
}else{
SYSTEM.OUT.PRINTLN ("Multiple parameters passed in, each parameter is as follows:");
for (int i=0;i<s.length;i++) {
System.out.println ("the" + (i+1) + "parameters are" +s[i]+ ";");
}
System.out.println ();
}
}

public static void Testintegerargs (Integer ... ints) {
if (ints.length==0) {
System.out.println ("0 integer parameters passed in");
}else if (ints.length==1) {
System.out.println ("1 integer parameters passed in");
}else{
SYSTEM.OUT.PRINTLN ("Multiple parameters passed in, each parameter is as follows:");
for (int i=0;i<ints.length;i++) {
System.out.println ("the" + (i+1) + "integer parameter is" +ints[i]+ ";");
}
System.out.println ();
}
}

Operation Result:
0 parameters passed in
1 parameters passed in
Multiple parameters are passed in, each with the following parameters:
The 1th parameter is one;
The 2nd parameter is two;
The 3rd parameter is three;
//
Multiple parameters are passed in, each with the following parameters:
The 1th parameter is one;
The 2nd parameter is two;
The 3rd parameter is three;
//
0 integer parameters passed in
1 integer parameters passed in
Multiple parameters are passed in, each with the following parameters:
The 1th integer parameter is 1;
The 2nd integer parameter is 2;
The 3rd integer parameter is 3;
//
Multiple parameters are passed in, each with the following parameters:
The 1th integer parameter is 1;
The 2nd integer parameter is 2;
The 3rd integer parameter is 3;

"Java Learning Note 27" method for Java8 multiple parameters

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.