A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lh994749769 中级黑马   /  2015-3-2 20:51  /  735 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package generic;

  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.util.ArrayList;
  5. import java.util.ListIterator;

  6. /**
  7. @author LiHang
  8. @date: 2015年3月2日下午6:25:44
  9. */
  10. public class Generic {

  11.         public Generic() {
  12.                 // TODO Auto-generated constructor stub
  13.         }
  14.         public static void main(String[] args){
  15.                 /*ArrayList arr = new ArrayList();
  16.                 arr.add(4);
  17.                 arr.add("fadfe");
  18.                 System.out.println(arr);*/
  19.                 /*
  20.                  *
  21.                  * 需求:在Integer类型下的ArrayList集合中添加String类型的数据
  22.                  * 因为ArrayList已经被指定为Integer类型,所以就要得到,它未被指定类型的add方法,也就是add(Object),
  23.                  * 那么就要用到反射,
  24.                  *
  25.                  * */
  26.                 ArrayList<Integer> arrInteger = new ArrayList<Integer>();
  27.                 Method add = null;
  28.                 try {
  29.                         add = arrInteger.getClass().getMethod("add", Object.class);
  30.                 } catch (NoSuchMethodException | SecurityException e) {
  31.                         // TODO Auto-generated catch block
  32.                         e.printStackTrace();
  33.                 }
  34.                 try {
  35.                         add.invoke(arrInteger, "stringToArrInteger");
  36.                 } catch (IllegalAccessException | IllegalArgumentException
  37.                                 | InvocationTargetException e) {
  38.                         // TODO Auto-generated catch block
  39.                         e.printStackTrace();
  40.                 }
  41.                 System.out.println(arrInteger);
  42.                 arrInteger.add(2);
  43.                 arrInteger.add(3);
  44.                 arrInteger.add(4);
  45.                 arrInteger.add(5);
  46.                 arrInteger.add(6);
  47.                 //输出方式一
  48.                 for(int i = 0;i<arrInteger.size();i++){
  49.                         sop(arrInteger.get(i));
  50.                 }
  51.                 //输出方式二
  52.                 for(ListIterator it = arrInteger.listIterator();it.hasNext();){
  53.                         sop(it.next());
  54.                 }
  55.                 sop(arrInteger);
  56.         }

  57.         /*public static <T> void sop(T t){
  58.                 System.out.println(t.getClass().getName()+"::"+t);
  59.         }*/
  60.         //打印语句
  61.         public static void sop(Object t){
  62.                 System.out.println(t.getClass().getName()+"::"+t);
  63.         }

  64. }
复制代码
上面代码是将一个Sting类型的数据加入到ArrayList<integer>集合中,这么写是没有问题的,但是当把sop打印语句改为、
  1. public static <T> void sop(T t){
  2.                 System.out.println(t.getClass().getName()+"::"+t);
  3.         }
复制代码
的时候最会报String类型转换为Integer异常,实在想不通是为什,求解答


1 个回复

倒序浏览
是第52行出现的问题
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马