问题就是<>需要加在哪里?使用中在类型声明和new后面要加,比如:MyList<String> aList = new MyList<String>();
定义类的时候加在哪里呢?
class MyList<T>{
  T aObj;
  ...
}
这样子没错吧。可是经常看到类似下面的代码,又是什么意思呢?public static <K,V> Map<K,V> newHashMap() {
    ...
}Map不是函数的返回类型吗?怎么两边都有<>?

解决方案 »

  1.   

    class MyList<T>{
      T aObj;
      ...
    }
    不对啊
    <T>{非法
      

  2.   

    很有趣的问题,探讨中。希望楼主把出处贴出来,或者多贴几段代码。下面是我作的测试。
    当前能通过编译,其它被屏蔽的都通不过。import java.util.*;public class TestGenericSyntax {
        static String  s = "Hello";
        static Integer I = 0;    public static <String, Integer> Map<String, Integer> newHashMap1() {
            
            HashMap<String, Integer> map = new HashMap<String, Integer>();        // err msg
            // symbol  : constructor HashMap(java.util.Map<java.lang.String,java.lang.Integer>)
            map = 
                new HashMap<String, Integer>(newHashMap2());        // err msg
            // TestGenericSyntax.java:10: cannot find symbol
            // symbol  : method valueOf(int)
            // map.put("1", Integer.valueOf(1));        // err msg
            // TestGenericSyntax.java:14: cannot find symbol
            // symbol  : method put(java.lang.String,java.lang.Integer)
            // map.put(s, I);        // err msg        
            // TestGenericSyntax.java:15: incompatible types
            // map = newHashMap2();        return map;
        }    public static Map<String, Integer> newHashMap2() {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("1", Integer.valueOf(1));        // err msg
            // symbol  : constructor HashMap(java.util.Map<java.lang.Object,java.lang.Object>)
            // map = new HashMap<String, Integer>(newHashMap1());        map = new HashMap<String, Integer>(newHashMap3());        return map;
        }    public static Map<String, Integer> newHashMap3() {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("1", Integer.valueOf(1));
            return map;
        }    public static void main(String[] args) {
        }
    }
      

  3.   

    public static <String, Integer> Map<String, Integer> newHashMap1() {        ...        // err msg
            // symbol  : constructor HashMap(java.util.Map<java.lang.String,java.lang.Integer>)
           
            map = 
               new HashMap<String, Integer>(newHashMap2());这一条也通不过,忘屏蔽了。