List,Vector之类使用时的提醒问题

来源:互联网 发布:企业工商数据查询 编辑:程序博客网 时间:2024/05/18 02:15
Eclipse3.2中以下代码在.add方法被调用时会出现警告(warnings)
     ArrayList queryList = new ArrayList();
     queryList.add(myForm);
 提醒内容如下:Type safety: The method add(Object) belongs to the raw
              type ArrayList. References to generic type
              ArrayList<E> should be parameterized
 这是因为JDK 1.5版本的新功能,需要对List,Vector之类定义时候,最好进行泛化。
 定义类型,如:Vector<String> v = new Vector<String>();
 这样的好处是再你add("abc");后想获取这个元素不需要再转化了。
 如,v.get(0)将返回的就是一个String对象。
 虽然没有发现这样做的好处,不过这样可以去掉恶心的警告(warnings)了。
 上面代码变成这样就没有那个警告(warnings)了。
     ArrayList<myForm> queryList = new ArrayList<myForm>();
     queryList.add(myForm);
 
原创粉丝点击