黑马程序员技术交流社区
标题:
泛型的问题
[打印本页]
作者:
天方地圆
时间:
2014-4-3 16:18
标题:
泛型的问题
本帖最后由 天方地圆 于 2014-4-5 22:03 编辑
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.net.Proxy.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
public class GenericTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
/*
ArrayList<String> collection = new ArrayList<String>();
collection.add("abc");
String element = collection.get(0);
System.out.println(element);
ArrayList<Integer> collection2 = new ArrayList<Integer>();
System.out.println(collection2.getClass() == collection.getClass() );
collection2.getClass().getMethod("add", Object.class).invoke(collection2, "abc");
System.out.println(collection2.get(0));
HashMap<String,Integer> maps = new HashMap<String,Integer>();
maps.put("anadgn", 30);
maps.put("dsjgjdsg", 40);
maps.put("abdg", 50);
Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
for(Map.Entry<String,Integer> entry : entrySet){
System.out.println(entry.getKey() +":"+ entry.getValue());
}
swap(new String[]{"abc","bdg","itcast"},1,2);
//swap(new int[]{1,23,4,5});
Object obj = "abc";
String x3 = autoConvert(obj);
*/
//Vector<Date> v1 = new Vector<Date>();
Method applyMethod = GenericTest.class.getMethod("applyMethod",Vector.class);
java.lang.reflect.Type[] types = applyMethod.getGenericParameterTypes();
ParameterizedType pType = (ParameterizedType)types(0);
System.out.println(pType.getRawType());
System.out.println(pType.getActualTypeArguments()[0]);
}
public static void applyMethod(Vector<Date> v1){
}
}
复制代码
作者:
天方地圆
时间:
2014-4-3 16:20
运行报出,空指针异常,是怎么回事?
作者:
一年_Hei
时间:
2014-4-3 16:38
第51行types(0);改为types[0];
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2