標籤:
在java中,可以聲明一個泛型數組,不能通過直接通過T[] tarr=new T[10]的方式來建立數組,最簡單的方式便是通過Array.newInstance(Classtype,int size)的方式來建立數組例如下面的程式。
public class ArrayMaker<T> {
private Class<T> type;
public ArrayMaker(Class<T> type) {
this.type = type;
}
@SuppressWarnings("unchecked")
T[] createArray(int size) {
return (T[]) Array.newInstance(type, size);
}
List<T> createList() {
return new ArrayList<T>();
}
/** * @param args */
public static void main(String[] args) {
/** Even though kind is stored as Class<T> , erasure means that it is actually just being stored as a Class, with
* no parameter. So, when you do some thing with it, as in creating an array, Array.newInstance( ) doesn’t
* actually have the type information that’s implied in kind; so it cannot produce the specific result, which
* must therefore be cast, which produces a warning that you cannot satisfy.
*/
ArrayMaker<Type> am2 = new ArrayMaker<Type>(Type.class);
System.out.println(Arrays.asList(am2.createArray(10)));
System.out.println(Arrays.asList(am2.createList()));
}
}
class Type {
@Override
public String toString() {
return "type";
}
}
程式一:這個程式主要說明了,在使用泛型數組中容易出現的問題,由於書中對於程式的說明比較詳細,所以只對程式做引用。
class Generic<T> {
}
public class ArrayofGeneric {
public static void main(String[] args) {
Generic<Integer>[] genArr;
genArr = (Generic<Integer>[]) new Generic[2];
System.out.println(genArr);
}
}
程式二:這個程式主要是說明在程式的執行過程中,泛型數組的類型資訊會被擦除,且在啟動並執行過程中數組的類型有且僅有Object[],如果我們強制轉換成T[]類型的話,雖然在編譯的時候不會有異常產生,但是運行時會有ClassCastException拋出。
public class ArrayOfGeneric2<T> { public T[] ts; public ArrayOfGeneric2(int size) { ts = (T[]) new Object[size]; } public T get(int index) { return ts[index]; } public T[] rep() { return ts; } public void set(int index, T t) { ts[index] = t; } public static void main(String[] args) {
ArrayOfGeneric2<String> aog2 = new ArrayOfGeneric2<String>(10); Object[] objs = aog2.rep(); System.out.println(objs); /* will throw ClassCastException */ // String[] strs = aog2.rep(); //System.out.println(strs); } }
程式三:主要說明在對象中通過用Object[]來儲存資料,則產生對象是,可以對其持有的對象在T和object之間進行轉換,但是當設計到數組的轉換時,還是會報ClassCastException
public class ArrayOfGeneric3<T> { Object[] ts; public ArrayOfGeneric3(int size) { ts = new Object[size]; } public T get(int index) { return (T) ts[index]; } public T[] rep() { return (T[]) ts; } public void set(int index, T t) { ts[index] = t; } public static void main(String[] args) {
ArrayOfGeneric3<Integer> aog2 = new ArrayOfGeneric3<Integer>(10); Object[] objs = aog2.rep(); for (int i = 0; i < 10; i++) { aog2.set(i, i); System.out.println(aog2.get(i)); } Integer[] strs = aog2.rep(); System.out.println(strs); } }
程式四:是對泛型數組相對而言比較完美的解決方案
public class ArrayOfGeneric4<T> { T[] ts; public ArrayOfGeneric4(Class<T> type, int size) { /* to solution array of generic key code! */ ts = (T[]) Array.newInstance(type, size); } public T get(int index) { return ts[index]; } public T[] rep() { return ts; } public void set(int index, T t) { ts[index] = t; } public static void main(String[] args) {
ArrayOfGeneric4<Integer> aog2 = new ArrayOfGeneric4<Integer>(Integer.class, 10); Object[] objs = aog2.rep(); for (int i = 0; i < 10; i++) { aog2.set(i, i); System.out.println(aog2.get(i)); } try { Integer[] strs = aog2.rep(); System.out.println("user Array.newInstance to create generci of array was successful!!!!! "); } catch (Exception ex) { ex.printStackTrace(); } } }
原文請見http://developer.51cto.com/art/201202/317813.htm
Java中泛型數組建立總結