In Java, variable arity methods (methods with variable arguments) use an elipsis (...) to indicate that the method accepts a variable number of arguments. When you pass an array to a variable arity method, the array elements are treated as individual arguments to that method.
Consider the following code:
class A { private String extraVar; public String myFormat(String format, Object... args) { return String.format(format, extraVar, args); } }
In this example, the myFormat method takes a variable number of arguments (represented by the ... args) and formats a string using the String.format method. The issue is that args is treated as a single Object[] array, rather than as individual arguments. To address this, you can leverage the fact that a T... is syntactic sugar for a T[].
Java Language Specification 8.4.1 states:
"If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method."
To illustrate this, the following code snippet demonstrates how to pass an array as arguments to a variable arity method:
public static String ezFormat(Object... args) { String format = new String(new char[args.length]).replace("\0", "[ %s ]"); return String.format(format, args); } public static void main(String... args) { System.out.println(ezFormat("A", "B", "C")); // prints "[ A ][ B ][ C ]" }
In this example, the ezFormat method takes a variable number of arguments and returns a formatted string. Because String.format is also a variable arity method, each argument passed to ezFormat is treated as an individual argument to String.format.
When passing null values to a variable arity method, it's important to be aware that the last statement in the following example:
static void count(Object... objs) { System.out.println(objs.length); } count(null, null, null); // prints "3" count(null, null); // prints "2" count(null); // throws java.lang.NullPointerException!!!
...will result in a NullPointerException. To avoid this, you can pass the null argument as a separate array or as an Object, like this:
count(new Object[] { null }); // prints "1" count((Object) null); // prints "1"
To add extra arguments to an array that is passed to a variable arity method, you can use helper methods like append() and prepend() to modify the array:
staticT[] append(T[] arr, T lastElement) { final int N = arr.length; arr = java.util.Arrays.copyOf(arr, N 1); arr[N] = lastElement; return arr; } static T[] prepend(T[] arr, T firstElement) { final int N = arr.length; arr = java.util.Arrays.copyOf(arr, N 1); System.arraycopy(arr, 0, arr, 1, N); arr[0] = firstElement; return arr; }
Variable arity methods only work with reference types. Autoboxing does not apply to arrays of primitives. To pass an array of primitives, you must first convert them to an array of their wrapper type:
int[] myNumbers = { 1, 2, 3 }; System.out.println(ezFormat(myNumbers)); // prints "[ [I@13c5982 ]" Integer[] myNumbers = { 1, 2, 3 }; System.out.println(ezFormat(myNumbers)); // prints "[ 1 ][ 2 ][ 3 ]"
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3