Java record-87-java. lang. reflect. Array

Source: Internet
Author: User

Java record-87-java. lang. reflect. Array
The java. lang. reflect. Array class provides various static methods for dynamically creating and accessing Array elements.

Import java. lang. reflect. Array; public class ArrayTester1 {public static void main (String [] args) throws Exception {Class <?> ClassType = Class. forName ("java. lang. string "); Object array = Array. newInstance (classType, 10); Array. set (array, 5, "hello"); String str = (String) Array. get (array, 5); System. out. println (str); // hello} public class ArrayTester2 {public static void main (String [] args) throws Exception {int [] dims = new int [] {5, 10, 15}; Object array = Array. newInstance (Integer. TYPE, dims); // generates a three-dimensional array with dimensions of 5, 10, and 15. // System. out. println (array instanceof int [] [] []); // true Object arrayobj = Array. get (array, 3); // obtain the third element of the first dimension, that is, a two-dimensional array // System. out. println (arrayobj instanceof int [] []); // true arrayobj = Array. get (arrayobj, 5); // get a one-dimensional Array. set (arrayobj, 10, 37); int [] [] [] arraycast = (int [] [] []) array; System. out. println (arraycast [3] [5] [10]); // System. out. println (Integer. TYPE); // int // System. out. println (Integer. class); // class java. lang. integer }}

 

 

Related Article

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.