"Thinkinginjava" 38, generic array conversion, passing a type identifier

Source: Internet
Author: User

/*** Book: Thinking in Java * function: Generic array conversion, passing a type identity * file: genericarraywithtypetoken.java* time: April 19, 2015 10:05:43* Author: cutter_ Point*/package Lesson15_generices;import Java.lang.reflect.array;public class genericarraywithtypetoken<t>{ Private t[] array;//an array of type T, which is actually the public genericarraywithtypetoken of type object in the JVM (class<t> type, int sz) {array = (t []) array.newinstance (type, SZ);//Create an array}//put the data public void put (int index, T item) {Array[index] = Item;} Get the corresponding data public T get (int index) {return (T) Array[index];} Returns the array public t[] Rep () {return array;} public static void Main (string[] args) {genericarraywithtypetoken<integer> gai = new Genericarraywithtypetoken <Integer> (Integer.class, 10);//pass in a type parameter so that you can recover from erase//put 1 to 10for (int i = 0; i < ten; ++i) gai.put (i, I); for (int i = 0; I < 10; ++i) System.out.print (Gai.get (i) + ""); System.out.println (); integer[] ia = Gai.rep (); SYSTEM.OUT.PRINTLN (IA);}}


Output:

0 1 2 3 4 5 6 7 8 9
[ljava.lang.integer;@4d4bb075





"Thinkinginjava" 38, generic array conversion, passing a type identifier

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.