Java reflection - use of array class

Mssyaa 2022-01-26 15:53:32 阅读数:60

java reflection use array class

It goes without saying that everyone knows java.lang.Array It's right Java A class that reflects array operations in a package .JavaSE8 In your document Array My description goes like this :

The Array class provides static methods to dynamically create and access Java arrays.

Array Class provides static methods for dynamic creation and access Java Array . Access is not difficult to understand , Dynamic creation can take a closer look at .

Let's see first java.util.Arrays

  • Note that Arrays, I believe some friends have used this tool class many times , It provides many methods for array operation, which is convenient for us to use .

  • It says java.lang.Array It provides us with static methods to dynamically create and access arrays . Let's see Arrays Medium copyOf How to dynamically manipulate arrays .

copyOf What is it for ?Arrays This method is mainly provided to expand the size of the filled array .

You can use

  I don't know if you've noticed , This method is a generic return result . Its first argument is the original array , The second parameter is the new length , Returns a call to another overloaded copyOf Method , Let's take a look at this overloaded copyOf Methods! .

  • The call inside is not difficult to understand , If it comes in original Object array Class and Object[] Of Class Equal, then directly new Object[] If not, call java.lang.reflect.Array Medium newInstance Method to create a new array , The next call is System.arraycopy Method, the comments in the source code are :Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. intend : Copy from the specified position of the specified array to the specified position of the target array .

Why use reflection to expand arrays

  • Let's take a look at the implementation without reflection "copyOf"

  • Without the one above Arrays Of copyOf 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 ? Say so , One MyObject[] Class into Object[], Then it's OK to turn back , But from the beginning Object[] The array of cannot be converted to MyObject[], Doing so will throw ClassCastException abnormal , This is because this array uses new Object[length] Created ,Java When creating an array, remember the type of each element , Is in the new The type of time .

  • So how can we make a strong turn ? Look at the code below

Read the code above , Some friends will have questions , Why use object Receive array objects , This is because arrays of basic data types cannot be passed to object arrays , But it can be turned into an object

Access the objects in the array

  • Array Class provides some methods for us to use


  The complete code is as follows





copyright:author[Mssyaa],Please bring the original link to reprint, thank you.