eclipse下使用泛型遇到问题解决办法2011-08-26java中泛型出现在j2se5以后的版本中,开发平台:eclipse3.2+jdk1.6,在一次使用java泛型中遇到IDE工具报错,后来经过查找原因是:eclipse3.2默认的编译是使用1.4造成不可以使用java的泛型。解决办法:在eclipse的菜单中的window——>perfernces——>java—— >compiler——>compiler compliance lever中把1.4改为5.0或6.0(根据jdk版本定)。例子:
1package fanxing;
2
3import java.util.Hashtable;
4
5public class Good<K,V> {
6
7 /** *//**
8 * @param args
9 */
10 public Hashtable<K,V> h=new Hashtable<K,V>();
11 public void put(K k, V v) {
12 h.put(k,v);
13 }
14 public V get(K k) {
15 return h.get(k);
16 }
17
18 public static void main(String[] args) {
19 // TODO Auto-generated method stub
20 Good<String, String> tg = new Good<String, String>();
21 tg.put("key", "value");
22 System.out.println(tg.get("key"));
23
24
25 }
26
27}
28
输出:value