- Creating a generic array directly cannot be compiled, whereas a transformed object array is compiled but not run in the JVM
-
public class arrayofgeneric{ static generic<integer>[] gia; @SupperssWarnings ("unchecked") public static void Main (string[] args) { Gia = (generic<integer>[]) New GENERIC[100]; Convert an anonymous object by Type //! gia[0] = new Object ();//compilation does not pass, cannot (directly) create a generic array instance }}
- The problem is that the array will track their actual type, which is determined when the array is created, so even though Gia has been transformed to generic<integer>[], this information only exists at compile time ( And if there is no @suppresswarning ("unchecked") annotations, you will get a warning of this transformation). At run time, it is still an object array
- Therefore, the only way to successfully create a generic array is to create a new array of the erased type, and then transform it (and transform it at run time)
- Cast directly on the entire array, and will still be erased at compile time! So it should be transformed at runtime, and the best way to do this is to use a generic array wrapper to maintain an array of primitive types, type-safe detection of element compilation periods (corresponding return values) and coercion of type conversions using the array entry method ( is not important for the runtime, thus ensuring type safety.
-
- Example of a forced transformation of an entire array (Error method)
-
public class Genericarray<t> { private t[] array; @SupperessWarning ("unchecked") public genericarray (int sz) { array = (t[]) new Object[sz]; } public void put (int index, T item) { Array[index] = Item; } Public T get (int index) {return array[index];} Public t[] Rep () {return array;}//should be at runtime exit fuss public static void Main (string[] args) { Genericarray<intege r> gai = new genericarray<integer> (ten); Integer[] ia = Gai.rep (); ClassCastException object[] oa = Gai.rep ();//can only return an object array of type object[]
- The actual runtime object array is object[], while the actual run-time array object may be of type T.
-
- Therefore, at runtime, the export of the array object should be transformed output, the Ingress method has been implemented type safety at compile time, so the export method can be assured that the type conversion, to ensure success. The following
-
public class Genericarray2<t> {private object[] A Rray; Maintain object[] Type array @SupperessWarning ("unchecked") public GenericArray2 (int sz) {array = new OBJECT[SZ]; } public void put (int index, T item) {Array[index] = Item; Public T get (int index) {return (T) Array[index];} Array object export strong to public t[] rep () {return (t[]) array;}//Runtime No matter what is object[] type public static void Main (string[] args) { genericarray<integer> gai = new genericarray<integer> (10); Integer[] ia = Gai.rep (); Still classcastexception object[] oa = Gai.rep (); Only the object array type object[] Gai.put (0,11) can be returned; System.out.println (gai.get (0)); 11, Export Successful Transition}}
- An array of objects of the actual type type[] is created by reflection at run time, avoiding type erasure, thus converting successfully, without classcastexception
Import java.lang.reflect.*; public class Genericarraywithtypetoken<t> { private t[] array; @SuppressWarning ("unchecked") public Genericarraywithtypetoken (class<t> type, int sz) { array = (t[]) Array.newinstance (Type, SZ),//The object array with actual type type[] is created by reflection at runtime, avoids type erasure, thus converting succeeds, without classcastexception} public void put (int index, T item) { Array[index] = Item; } Public T get (int index) {return array[index];} Public t[] Rep () {return array;} Can successfully return ~ public static void Main (string[] args) { genericarraywithtypetoken<integer> gawtt = new Genericarraywithtypetoken<> (Integer.class, ten); Integer[] ia = Gawtt.rep (); Can be successfully returned! }}
- Conclusion
- Cannot (directly) create a generic array
- The actual run-time object array of a generic array can only be the original type (t[] is object[],pair<t>[]), while the actual runtime array object may be of type T (although the runtime will erase to the original type)
- General solution: (Generic array wrapper): Use ArrayList to collect object elements of generic array objects, such as arraylist<t>, arraylist<pair<string>>
- Gets the behavior of the array, and the type safety of the compile period provided by the generic
Java generic Generic array