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