Java variable parameter usage: 1. When the method accepts multiple parameters, use the "printNumbers" method to accept any number of integer parameters and print them out one by one; 2. The method accepts multiple parameters, some of which The parameters are fixed. Use the printInfo method to first print the name, and then print each element in the scores array; 3. The method accepts a variable number of parameters of the same type, and uses the sum method to accept any number of integer parameters and accumulate them. Return later.
# Operating system for this tutorial: Windows 10 system, Dell G3 computer.
Java's variable parameters are a special syntax that allows a method to accept an indeterminate number of parameters. The use of variable parameters can greatly simplify method calls and improve the flexibility and readability of the code. Below I will introduce the usage of Java variable parameters in detail.
The syntax of variable parameters is expressed by using three consecutive dots (...) in the parameter list of the method, for example:
public void methodName(Type... parameterName) { // 方法体 }
Among them, Type is the data type of the variable parameters, parameterName is the name of the variable parameter.
Inside the method, the variable parameters can be treated as an array, and any array-related operations can be used, such as traversing, getting the length, accessing elements at specific positions, etc.
The following are some common usage scenarios and examples of variable parameters:
1. When a method accepts multiple parameters:
public static void printNumbers(int... numbers) { for (int number : numbers) { System.out.println(number); } } // 调用方法 printNumbers(1, 2, 3, 4, 5);
Above In the code, the printNumbers method can accept any number of integer parameters and print them out one by one.
2. The method accepts multiple parameters, some of which are fixed:
public static void printInfo(String name, int... scores) { System.out.println("Name: " + name); System.out.println("Scores:"); for (int score : scores) { System.out.println(score); } } // 调用方法 printInfo("John", 90, 80, 70);
In the above code, the printInfo method accepts a string parameter name and any number of Integer parameter scores. Inside the method, first print name, then print each element in the scores array.
3. The method accepts a variable number of parameters of the same type:
public static int sum(int... numbers) { int total = 0; for (int number : numbers) { total += number; } return total; } // 调用方法 int result = sum(1, 2, 3, 4, 5);
In the above code, the sum method accepts any number of integer parameters and accumulates them. Return later.
It should be noted that the variable parameter must be the last parameter in the method parameter list. If the method has other parameters, the variadic parameters must be placed last.
In addition, if no parameters are passed in when calling a variable parameter method, the variable parameters will be treated as an empty array instead of null.
When writing a method that uses variable parameters, you need to consider the legality of the parameters and the handling of empty arrays to avoid exceptions.
The above is the detailed content of How to use variable parameters in java. For more information, please follow other related articles on the PHP Chinese website!