Java Variable Length Parameter

Source: Internet
Author: User

The variable length parameter is provided in Java 5, that is, you can use a number of uncertain parameters in the method definition. You can call different numbers of parameters for the same method, for example: print ("hello"); print ("hello", "lisi"); print ("hello", "James "); the following describes how to define variable length parameters and how to use variable length parameters.

1. Variable Length Parameter Method Definition

Variable Length Parameter, such

Print (String... args ){

...

}

Parameters can be used as arrays in methods with variable length parameters. For example, all parameter values can be output cyclically.

Print (String... args ){

For (String temp: args)

System. out. println (temp );

}

2. Call methods with Variable Length Parameters

Multiple parameters can be provided during the call, for example:

Print ("hello ");

Print ("hello", "lisi ");

Print ("hello", "James ");

3. Notes

3.1 When calling a method, if it can match a method with a fixed parameter or a method with a variable length parameter, select a method with a fixed parameter. See the output of the following code:

Package ch6;

Import static java. lang. System. out;
Public class vararstest {

Public static void main (String [] args ){
Vararstest test = new vararstest ();
Test. print ("hello ");
Test. print ("hello", "zhangsan ");
}
 
Public void print (String... args ){
For (int I = 0; I <args. length; I ++ ){
Out. println (args [I]);
}
}
 
Public void print (String test ){
Out. println ("----------");
}
}

3.2 If the method to be called can match two variable parameters, an error occurs, for example, the following code:

Package ch6;

Import static java. lang. System. out;
Public class vararstest {

Public static void main (String [] args ){
Vararstest test = new vararstest ();
Test. print ("hello ");
Test. print ("hello", "zhangsan ");
}
 
Public void print (String... args ){
For (int I = 0; I <args. length; I ++ ){
Out. println (args [I]);
}
}
 
Public void print (String test, String... args ){
Out. println ("----------");
}
}

For the above Code, neither of the two calls in the main method can be compiled.

3.3 A method can have only one variable length parameter, and the variable length parameter must be the last parameter of the method.

The following two methods are incorrectly defined.

Public void test (String... strings, ArrayList list ){

}
 
Public void test (String... strings, ArrayList... list ){

}

The following is an example with traps (for example, html "> outputs). Write the output result:

Public class TestVarargs {
Public static void m (String [] ss ){
For (int I = 0; I <ss. length; I ++ ){
System. out. println (ss [I]);
}
}
Public static void m1 (String s, String... ss ){
For (int I = 0; I <ss. length; I ++ ){
System. out. println (ss [I]);
}
}
Public static void main (String [] args ){
String [] ss = {"aaa", "bbb", "ccc "};
M (ss );
M1 ("");
M1 ("aaa ");
M1 ("aaa", "bbb ");
}
}

Finished!

Li xucheng CSDN Blog: http://blog.csdn.net/javaeeteacher
CSDN student base camp: http://student.csdn.net/space.php? Uid = 1, 124362
If you like my article, add me as a friend: http://student.csdn.net/invite.php? U= 124362 & c = 7be8ba2b6f3b6cc5

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.