Needless to say, we all know that java.lang.Array is a class for Array operation in Java reflection package. The description of Array in the JavaSE8 document is as follows:
The Array class provides static methods to dynamically create and access Java arrays.
The Array class provides static methods to dynamically create and access Java arrays. Access is not difficult to understand, dynamic creation can take a closer look.
Let's look at java.util.Arrays first
-
Pay attention to Arrays. I believe some partners have used this tool class many times and provide many methods for array operation for our convenience.
-
As mentioned above, java.lang.Array provides us with static methods to dynamically create and access Arrays. Let's take a look at how the copyOf method in Arrays dynamically operates Arrays.
public static <T> T[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass()); }
What is copyOf for? Arrays mainly provides this method to expand the size of the filled array.
You can use it like this
User[] users = new User[10]; ...//If full, double the length of the array. users = Arrays.copyOf(users, users.length * 2);
I wonder if you have noticed that this method is a generic return result. The first parameter is the original array, and the second parameter is the new length. What is returned is that another overloaded copyOf method is called. Let's take a look at this overloaded copyOf method.
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
The call inside is not difficult to understand. If the Class of the original Object array passed in is equal to the Class of Object [], directly new Object []. If it is not equal, call the newInstance method in java.lang.reflect.Array to create a new array. The function of the System.arraycopy method is called later. The comment in the source code is: Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
Why use reflection to expand arrays
-
Let's look at "copyOf" without reflection
-
private static Object[] badCopyOf(Object[] arr, int newLength) { Object[] newArray = new Object[newLength]; System.arraycopy(arr, 0, newArray, 0, Math.min(arr.length, newLength)); return newArray; }
Without the copyOf method of Arrays above, many people may write the above code directly. But have you ever thought about a question, can it be transformed into the corresponding class you want to use? In this way, it is OK to convert a MyObject [] class to Object [] and then back, but the array that is Object [] from the beginning cannot be converted to MyObject []. Doing so will throw a ClassCastException exception because this array is created with new Object[length]. When creating a Java array, remember the type of each element, that is, the type at new.
So how can we make a strong turn? See the following code
-
private static Object goodCopyOf(Object arr, int newLength) { Class cls = arr.getClass(); if (!cls.isArray()) { return null; } Object newArray = Array.newInstance(cls.getComponentType(), newLength); System.arraycopy(arr,0,newArray,0,Math.min(Array.getLength(arr), newLength)); return newArray; }
After reading the above code, some small partners will wonder why they use object to receive array objects. This is because arrays of basic data types cannot be passed to object arrays, but can be converted into objects
-
double[] arr = {1.1, 1.2, 1.4, 12.2}; arr = (double[]) goodCopyOf(arr, 10);
Accessing objects within an array
- The Array class provides some methods for us to use
The complete code is as follows
package io.ilss.reflection; import java.lang.reflect.Array; import java.util.Arrays; /** * className ArrayTest * description ArrayTest * * @author feng * @version 1.0 * @date 2019-01-29 23:42 */ public class ArrayTest { public static void main(String[] args) { double[] arr = {1.1, 1.2, 1.4, 12.2}; arr = (double[]) goodCopyOf(arr, 10); System.out.println(Arrays.toString(arr)); String[] arr1 = {"aa", "bb", "cc"}; arr1 = (String[]) goodCopyOf(arr1, 10); System.out.println(Arrays.toString(arr1)); System.out.println("ClassCastException"); arr1 = (String[]) badCopyOf(arr1, 20); } private static Object[] badCopyOf(Object[] arr, int newLength) { Object[] newArray = new Object[newLength]; System.arraycopy(arr, 0, newArray, 0, Math.min(arr.length, newLength)); return newArray; } private static Object goodCopyOf(Object arr, int newLength) { Class cls = arr.getClass(); if (!cls.isArray()) { return null; } Object newArray = Array.newInstance(cls.getComponentType(), newLength); System.arraycopy(arr, 0, newArray, 0, Math.min(Array.getLength(arr), newLength)); return newArray; } }
Pay attention to my 5 K!!! Share more dry goods!!!