Source: bytes. In the previous Java code, you can use an object array to implement this function. Because all classes directly or indirectly inherit from object classes. Varargs. Java
| Package sample; Class A1 {} public class varargs {static void printarray (object [] ARGs) {for (Object OBJ: ARGs) system. out. print (OBJ + ""); system. out. println ();} public static void main (string [] ARGs) {printarray (new object [] {New INTEGER (47), new float (3.14 ), new Double (11.11)}); printarray (new object [] {"one", "two", "three "}); printarray (new object [] {New A1 (), new A1 (), new A1 ()});}} |
Result: 47 3.14 11.11 One Two Three sample. a1 @ a90653 sample. a1 @ de6ced sample. a1 @ c17164 here the printarray method uses the object array as the parameter, and uses the foreach syntax to traverse the array and print each object. 2. Java se5 implements the same method of variable parameter list. parameters can be defined as follows: (Object... ARGs) to achieve the same effect. Newvarargs. Java
Package sample; Class A {} public class newvarargs {static void printarray (object... ARGs) {for (Object OBJ: ARGs) system. out. print (OBJ + ""); system. out. println ();} public static void main (string [] ARGs) {printarray (New INTEGER (47), new float (3.14), new double (11.11 )); printarray (47, Printarray ("one", "two", "three "); Printarray (new A (), new A (), new ()); Printarray (object []) New integer [] {1, 2, 3, 4 }); Printarray (); } }
|
Result: 47 3.14 11.11 47 3.14 11.11 One Two Three sample. A @ a90653 sample. A @ de6ced sample. A @ c17164 1 2 3 4 The array is not explicitly used as the parameter, but the compiler will actually fill the array for you. Therefore, you can use the foreach syntax to traverse it. Note that in the second to last line, the compiler has found that it is an array, so it will not perform any conversion on it. The last line indicates that zero parameters can be passed. You can also specify the types of all variable parameters. The program below specifies that all variable parameters must be strings. Optionalarguments. Java
| Package sample; public class optionalarguments {static void F (string... trailing) {for (string S: trailing) system. out. print (S + ""); system. out. println ();} public static void main (string [] ARGs) {f ("one"); F ("two", "three"); F ();}} |
Result: The original type and packaging class can be used in the Variable Parameter List. Autoboxingvarargs. Java
| Package sample; public class autoboxingvarargs {public static void F (integer... ARGs) {for (integer I: ARGs) system. out. print (I + ""); system. out. println ();} public static void main (string [] ARGs) {f (New INTEGER (1), new INTEGER (2); F (3, 4, 5, 6, 7, 8, 9); F (10, new INTEGER (11), 12 );}} |
Results: 1 2 3 4 5 6 7 8 9 10 11 12 3. if the overload of the Variable Parameter List occurs, the compiler automatically calls the most suitable method for matching. Overloadingvarargs. Java
Package sample; public class overloadingvarargs {static void F (character... ARGs) {system. out. print ("first"); For (character C: ARGs) system. out. print (C + ""); system. out. println ();} static void F (integer... ARGs) {system. out. print ("second"); For (integer I: ARGs) system. out. print (I + ""); system. out. println ();} static void F (long... ARGs) {system. out. print ("third"); For (long l: ARGs) system. out. print (L + ""); system. out. println ();} static void F (double... ARGs) {system. out. print ("forth"); For (double D: ARGs) system. out. print (D + ""); system. out. println ();} public static void main (string [] ARGs) {f ('A', 'B', 'C'); F (1 ); F (2, 1); F (0.1); F ( } }
|
Result: First a B C second 1 second 2 1 forth 0.1 third 0 is not allowed, that is, a variable parameter is added to a method. Overloadingvarargs2.java
| Package sample; public class overloadingvarargs2 {static void F (float I, character... ARGs) {system. out. println ("first");} static void F (character... ARGs) {system. out. println ("second");} public static void main (string [] ARGs) {f (1, 'A'); F ('A', 'B '); // error }} |
But the problem can be solved in this way.
| Package sample; public class overloadingvarargs2 {static void F (float I, character... ARGs) {system. out. println ("first");} static void F (char C, character... ARGs) {system. out. println ("second");} public static void main (string [] ARGs) {f (1, 'A'); F ('A ', 'B ');}} |
That is, the overloaded methods must be in the same parameter format.