/*** 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