Eclipse3.2中以下代碼在.add方法被調用時會出現警告(warnings)
Vector v = new Vector ();
v.add(s);
提醒內容如下:Type safety: The method add(Object) belongs to the raw type Vector. References to generic type Vector<E> should be parameterized
這是因爲JDK 1.5版本的新功能,需要對List,Vector之類定義時候,最好進行泛化。
定義類型,如:Vector<String> v = new Vector<String>();
這樣的好處是再你add("abc");後想獲取這個元素不需要再轉化了。
如,v.get(0)將返回的就是一個String對象。
雖然沒有發現這樣做的好處,不過這樣可以去掉噁心的警告(warnings)了。
上面代碼變成這樣就沒有那個警告(warnings)了。
Vector <String> v = new Vector <String> ();
v.add(s);