Java Reflection (10): Arrays

Source: Internet
Author: User

Reprinted from Concurrent Programming network –ifeve.com

Content index
Java.lang.reflect.Array
Create an array
Access an array
Gets the class object of the array
Gets the member type of the array

Using a reflection mechanism to manipulate arrays can be tricky. Especially when you want to get an array of class objects, such as int[] and so on. This section discusses creating arrays through the reflection mechanism and how to get an array of class objects.

Note: The content of this article was updated after reading Eyal Lupu's blog post "The Side Notes about Arrays and Reflection". This version now references the content of this blog post.

Java.lang.reflect.Array

The Java reflection mechanism handles arrays by Java.lang.reflect.Array this class. Instead of confusing this class with Java.util.Arrays in the Java Collection Suite (Collections suite), Java.util.Arrays is a class that provides tool methods such as iterating through an array, converting an array to a collection, and so on.

Create an array

The Java reflection mechanism creates arrays through the Java.lang.reflect.Array class. Here is an example of how to create an array:

int [] Intarray = (int[]) array.newinstance (int. ) Class, 3);

This example creates an array of type int. The first parameter of the Array.newinstance () method represents what type of array we want to create. The second parameter indicates how large the space of this array is.

Access an array

The elements in the array can also be accessed through the Java reflection mechanism. You can use Array.get (...) specifically. and Array.set (...) method to access it. Here is an example:

int [] Intarray = (int[]) array.newinstance (int. ) Class, 30, 1231, 4562, 789); System.out.println ("intarray[0] =" + array.get (intarray, 0)); System.out.println ("intarray[1] =" + Array.get (intarray, 1)); System.out.println ("intarray[2] =" + Array.get (Intarray, 2));

This example will output:

Intarray[0] = 123intarray[1] = 456intarray[2] = 789

Gets the class object of the array

When I write the scripting language of Butterfly DI container, I get a little bit of trouble when I want to retrieve the class object of an array by reflection. You can do this to get the class object of the array, if not by reflection:

Class Stringarrayclass = string[]. class;

It is not so easy to get a class object using the Class.forName () method. For example, you can get a class object of a native data type (primitive) int array like this:

Class Intarray = Class.forName ("[I");

The letter I in the JVM represents the int type, and the left ' [' represents what I want is an array of type int, and the same rule applies to other native data types.
There is a slight difference in the array of ordinary object types:

Class Stringarrayclass = Class.forName ("[Ljava.lang.String;");

Notice that the right side of ' [L ' is the class name, and the right side of the class name is a '; ' Symbol. The meaning of this is an array of the specified type.
It is important to note that you cannot get a class object of the native data type through the Class.forName () method. The following two examples will be reported classnotfoundexception:

Class IntClass1 = Class.forName ("I"= class.forname ("int");

I usually use the following method to get the normal object and the class object of the native object:

 Public Class getclass (String className) {  ifreturnint .  Class;   if return long. class ;  ...   return Class.forName (className);}

Once you have a class object of type, you have a way to easily get to the class object of its array, you can create an empty array from the specified type, and then use this empty array to get the class object of the array. It's a bit flattering, but it works. The following example:

Class Theclass == array.newinstance (theclass, 0). GetClass ();

This is a special way to get the class object of the specified array of the specified type. You do not need to use the class name or other means to get the class object.
To ensure that the class object does not represent an array, you can use the Class.isarray () method to verify:

Class Stringarrayclass = array.newinstance (String.  Class, 0). GetClass (); System.out.println ("is array:" + Stringarrayclass.isarray ());

Gets the member type of the array

Once you have the class object for an array, you can get the member type of the array through the Class.getcomponenttype () method. The member type is the data type that the array stores. For example, the member type of array int[] is a class object Int.class. The member type of string[] is the class object of the Java.lang.String classes.
The following is an example of accessing an array member type:

New String[3== stringarrayclass.getcomponenttype (); System.out.println (stringarraycomponenttype);

The following example prints "java.lang.String" to represent the member type of this array as a string.

Original address Author: Jakob Jenkov Translator: Yevenhai ([email protected])

Java Reflection (10): Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.