The following code is a way to implement a mutable parameter list.
public static void Printary (object[] objs) {for (Object obj:objs) { System.out.print (obj+ ""); } System.out.println (); } public static void Main (string[] args) { printary (new object[]{1,2,3,4,5}); Printary (New object[]{"ni", "Hao"});
Output Result:
In this way, you can pass any type, number of arguments to the function. However, the above method is a more outdated method, and after Java SE5 comes out, it provides a more convenient way.
The code is as follows:
public static void Printary (Object ... objs) {for (object obj:objs) { System.out.print (obj+ ""); } System.out.println (); } public static void Main (string[] args) { printary (1,2,3,4,5); Printary ("Ni", "Hao"); You do not need to explicitly create an array, which is populated automatically by the compiler. printary (New object[]{"ni", "Hao"});//You can also pass in an array. printary (); Nullable Printary (new Integer (1), New Float (2));//can pass different types of parameters }
Results:
The following code is a way to implement a mutable parameter list.
public static void Printary (object[] objs) {for (Object obj:objs) { System.out.print (obj+ ""); } System.out.println (); } public static void Main (string[] args) { printary (new object[]{1,2,3,4,5}); Printary (New object[]{"ni", "Hao"});
Output Result:
In this way, you can pass any type, number of arguments to the function. However, the above method is a more outdated method, and after Java SE5 comes out, it provides a more convenient way.
The code is as follows:
public static void Printary (Object ... objs) {for (object obj:objs) { System.out.print (obj+ ""); } System.out.println (); } public static void Main (string[] args) { printary (1,2,3,4,5); Printary ("Ni", "Hao"); You do not need to explicitly create an array, which is populated automatically by the compiler. printary (New object[]{"ni", "Hao"});//You can also pass in an array. printary (); Nullable Printary (new Integer (1), New Float (2));//can pass different types of parameters }
Results: