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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 黄奕豪 于 2012-5-27 15:10 编辑

泛型方法是这样:public<T> void printColl<Iterator<T> it>
通配符泛型是这样:public void printColl(Iterator<?>  it)
这两个方法在应用上都可以实现,他们有什么区别么?难道就一个简化书写?
好吧,我确实差,我不活了~~~重复了一遍视频,毕老师说了,泛型方法就多了一个能接收并操作这个类型。
  1. import java.util.*;
  2. class  GenericTest
  3. {
  4.         public static void main(String[] args)
  5.         {

  6.                 ArrayList<String> al = new ArrayList<String>();

  7.                 al.add("abc1");
  8.                 al.add("abc2");
  9.                 al.add("abc3");

  10.                 ArrayList<Integer> al1 = new ArrayList<Integer>();
  11.                 al1.add(4);
  12.                 al1.add(7);
  13.                 al1.add(1);

  14.                 printColl(al);
  15.                 printColl(al1);
  16.         }
  17.         public static <T> void printColl(Collection<T> al)
  18.         {
  19.                 Iterator<T> it = al.iterator();
  20.                 while(it.hasNext())
  21.                 {
  22.                         System.out.println(it.next());
  23.                 }
  24.         }
  25.         /*//通配符的方法
  26.         public static void printColl(Collection<?> al)
  27.         {
  28.                 Iterator<?> it = al.iterator();
  29.                 while(it.hasNext())
  30.                 {
  31.                         System.out.println(it.next());
  32.                 }
  33.         }*/
  34. }
复制代码

4 个回复

正序浏览
黄奕豪 发表于 2012-5-27 15:04
我一问完,我就在他的屁股底下找到了!~~~现在只能改贴了!谢谢啊!! ...

我刚才也发了个贴,我一问完,我也解决了,一个不小心的错误。
回复 使用道具 举报
王明(1988) 发表于 2012-5-27 14:59
使用ctrl+T找一下,但不表示他不存在,只能说明sun很猥琐啊,哈哈哈!

{:3_65:}我一问完,我就在他的屁股底下找到了!~~~现在只能改贴了!谢谢啊!!
回复 使用道具 举报
使用ctrl+T找一下,但不表示他不存在,只能说明sun很猥琐啊,哈哈哈!
回复 使用道具 举报
嗯,List集合的literator实现在AbstractList中,

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马