Java arguments are implemented by arrays.
object[] AddAll (object[] array1, Object ... array2) and Object[] AddAll (object[] array1, object[] array2) signatures should be consistent.
Public classArrayutils {//Clone// ----------------------------------------------------------------------- /** * <p> * Shallow clones an array returning a typecast result and handling <code>null</code> . * </p> * * <p> * The objects in the array was not cloned, thus there was no special handling for M ulti-dimensional arrays. * </p> * * <p> * This method returns <code>null</code> for a <code>null</cod E> input Array. * </p> * * @param array * The array to shallow clone, could be <code>null</code> * @return The cloned array, <code>null</code> if <code>null</code> input*/ Public Staticobject[] Clone (object[] array) {if(Array = =NULL) { return NULL; } returnArray.clone (); }/** * <p> * Adds All the elements of the given arrays into a new array. * </p> * <p> * The new array contains all of the element of <code>array1</code> followed B Y all of the elements <code>array2</code>. * When an array is returned, it's always a new array. * </p> * * <pre> * Arrayutils.addall (NULL, NULL) = null * Arrayutils.addall (array1, NULL) = cloned copy of array1 * Arrayutils.addall (null, array2) = cloned copy of Array2 * Arrayutils.addall ([], []) = [] * Arrayutils.addall ([null], [null]) = [NULL, NULL] * Arrayutils.addall (["A", "B", "C"], ["1", "2", " 3 "]) = [" A "," B "," C "," 1 "," 2 "," 3 "] * </pre> * * @param array1 * The first array whose Elements is added to the new array, could be <code>null</code> * @param array2 * The second Array whose elements is added to the new array, could be <cOde>null</code> * @return The new array, <code>null</code> if <code>null</code> array Inputs. The type of the new array is the type of the * first array. * @since 2.1*/ Public Staticobject[] AddAll (object[] array1, Object ... array2) {if(Array1 = =NULL) { returnClone (Array2); } Else if(Array2 = =NULL) { returnClone (Array1); } object[] Joinedarray=(object[]) Array. newinstance (Array1.getclass (). Getcomponenttype (), Array1.length+array2.length); System.arraycopy (Array1,0, Joinedarray,0, array1.length); System.arraycopy (Array2,0, Joinedarray, Array1.length, array2.length); returnJoinedarray; } /** * Swaps The specified elements in the specified array. * * @param arr * @param i * @param j*/ Public Static voidSwap (object[] arr,intIintj) {Object tmp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=tmp; }}
Test arguments
Public Static voidMain (string[] args) {string[] array= {" A"," the" }; //The argument of type string[] should explicitly be cast to object[] for the invocation of the VarArgs method //AddAll (object[], Object ...) from type arrayutils. It could alternatively is cast to Object for a varargs invocationSystem. out. println (Arrays.tostring (Arrayutils.addall (Array,NewString[] {"2","6"})));//Compile WarningSystem. out. println (Arrays.tostring (Arrayutils.addall (Array,"2","6")));//OKSystem. out. println (Arrays.tostring (Arrayutils.addall (array, (object[))NewString[] {"2","6"})));//OKSystem. out. println (Arrays.tostring (Arrayutils.addall (array, (Object)NewString[] {"2","6"})));//java.lang.ArrayStoreException}
Automatic packaging
Object[] AddAll (object[] array1, Object ... array2)
--parameters: object[] AddAll (object[] array1, Object Array2_0, Object Array2_1, Object Array2_2, ...)
--Variable length parameter packing: object[] array2 = new Object[]{array2_0, array2_1, array2_2};
--After compiling the actual: object[] AddAll (object[] array1, object[] array2)
Because the actual function is object[] AddAll (object[] array1, object[] array2), the compiler gives a warning when the array is passed directly in the variable-length parameter position.
- The default array is directly used as the second parameter of the actual function object[] AddAll (object[] array1, object[] array2),
- The elimination warning can add a cast, which is (object[]) the same as the default, and (object) as an item of the variable length parameter, automatically wrapped as new object[]{(object) New string[] {"2", "6"}}, which becomes a two-dimensional array, An array of type String cannot store an object of element string[] (string[] joinedarray; Joinedarray[0] = new string[] {"2", "6"} is incorrect), then a storage exception occurs in the second system.arraycopy
Another way to eliminate compilation warnings is through generics :
Public Static<T>object[] AddAll (object[] array1, T ... array2) {if(Array1 = =NULL) { returnClone (Array2); } Else if(Array2 = =NULL) { returnClone (Array1); } object[] Joinedarray=(object[]) Array. newinstance (Array1.getclass (). Getcomponenttype (), Array1.length+array2.length); System.arraycopy (Array1,0, Joinedarray, 0, array1.length); System.arraycopy (Array2,0, Joinedarray, Array1.length, array2.length); returnJoinedarray; }
The disadvantage is that if the array2 type is not all the same at compile time, it will be checked, as
String[] Array = {"$", "object[", "new " {"2", "6", 2
Continue to execute, will only get java.lang.ArrayStoreException. Unless the first parameter is object[] array = {"22", "66"}, the string can be stored and the integer can be stored (above 2)
Java variable parameter