Java Reflection Learning Three

Source: Internet
Author: User

Reflections and Arrays

  

  The Java.lang.Array class provides a variety of static methods for dynamically creating and accessing array elements.

The main () method of the ArrayTester1 class creates a string array of length 10, then sets the element with index position 5 to "hello", and then reads the value of the element with index position 5.

  

Import Java.lang.reflect.array;public class arraytester1{public    static void Main (string[] args) throws exception< c4/>{        class<?> ClassType = Class.forName ("java.lang.String");                Generates an array, specifying the element type and array length of        Object array = array.newinstance (ClassType, ten);                Array.set (Array, 5, "Hello");        String str = (string) array.get (Array, 5);                System.out.println (str);}                    }

Multidimensional arrays

First, distinguish between the following:

System.out.println (integer.type);   System.out.println (Integer.class);

Output:

Int

Class Java.lang.Integer

A program instance of a multidimensional array:

  

Import Java.lang.reflect.array;public class arraytester2{public static void Main (string[] args) {int[] dims        = new Int[] {5, 10, 15};    Note the following two kinds of System.out.println (integer.type) are differentiated;    int System.out.println (integer.class);        Integer//Create a three-dimensional array, the three dimensions of this array are 5,10,15 Object array = array.newinstance (Integer.type, dims);        Variable parameters can also be written like this://Object array = array.newinstance (Integer.type, 5,10,15);                System.out.println (array instanceof int[][][]);    class<?> CLASSTYPE0 = Array.getclass (). Getcomponenttype ();    Returns the array element type SYSTEM.OUT.PRINTLN (CLASSTYPE0);        The elements of a three-dimensional array are two-dimensional arrays, Output: class [I//Get an array of index 3 for the first layer, and return a two-dimensional array of Object Arrayobject = Array.get (array, 3);    class<?> ClassType = Arrayobject.getclass (). Getcomponenttype ();    Returns the array element type SYSTEM.OUT.PRINTLN (ClassType); The elements of a two-dimensional array are one-dimensional arrays, and the output: class [I//) returns a one-dimensional array arrayobject = Array.get (Arrayobject, 5);    class<?> classType2 = Arrayobject.getclass (). Getcomponenttype ();    Returns the array element type SYSTEM.OUT.PRINTLN (CLASSTYPE2);        The elements of a one-dimensional array are int//The position of a one-dimensional array labeled 10 is set at the value of Array.setint (Arrayobject, 10, 37);        Int[][][] Arraycast = (int[][][]) array;    System.out.println (arraycast[3][5][10]); }}
Resources:

Http://www.cnblogs.com/mengdd/archive/2013/01/26/2878136.html

Http://www.cnblogs.com/gulvzhe/archive/2012/01/27/2330001.html

Http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

Http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html


Java Reflection Learning Three

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.