我不明白这个<>符号怎么用,ArrayAdapter为什么后面加<String>,什么意思?

解决方案 »

  1.   

    那是泛型,JAVA5.0中的新特性,
    告别是在集合中框架中运用多。如List<String> list=new ArrsyList<String>();指定了此集合中只能存放String类型的数据。
    目的是为了安全,
    属于编译时语法。
      

  2.   

    对类的泛型, 我们看一下例子:
    public class Box<T> {
        private T t;
        
        private void add(T t) {
            this.t = t;
        }
        
        public T get() {
            return t;
        }
        
         public static void main(String[] args) {
            Box<Integer> boxInt = new Box<Integer>();
            boxInt.add(1);
            Integer intResult = boxInt.get();
            
            Box<String> boxStr = new Box<String>();
            boxStr.add("Test");
            String strResult = boxStr.get();
                    
            System.out.println("Integer Result : " + intResult);
            System.out.println("String Result : " + strResult);
        }
    }
    运行结果:Integer Result : 1
    String Result : Test是不是很有意思, 我们写了一个类, 但这个类的类型却不适固定的, 他即可以为String, 又可以为Integer, 可以为任意定义的类型(非primitive)。简单解释一下, T就是type, java定义的一个泛型的概念, 即不适关键字, 也不适类, 可以适应任何类型!e.g.:
    当我们传入String类型的时候, 那么T的类型就是String, 如果String strResult = boxStr.get(); 被替换成Integer strResult = boxstr.get();就会报错, 因为类型不匹配。这样的好处就是可以再代码中发现错误。
    这样的代码再我们日常编码中比较少见, 我们比较常用的只是对Collection类的一些泛型使用。但是再一些框架程序中, 我们经常可以看到这样编程的影子! 如easyMock等。 
      

  3.   

    泛型类型(generic type)参见:
    http://baike.baidu.com/view/1436058.htm
    http://www.ibm.com/developerworks/cn/java/j-djc02113/
      

  4.   

     挺不错!学习了,移植都不明白,knightzhuwei 这位帅哥讲的不错!