我正在扩展javax.swing.table.DefaultTableModel
,并且正在添加一个在类变量上调用Vector.set(...)
的方法。它导致unchecked
警告,我想修复而不是抑制。由于这不会延伸Vector
,我似乎无法使用<E>
,而且我无法知道Object
中Vector
的类型}}。建议?
方法:
/**
* Replace a row in the dataVector. Convenience method for
* getDataVector().set(index, element)
* @param rowNum the index of the row to replace
* @param replaceRow the element to be stored at the specified position
* @return the element previously at the specified position
* @throws ArrayIndexOutOfBoundsException if the index is out of range
*/
public Vector setRow(int rowNum, Vector replaceRow) {
return (Vector)dataVector.set(rowNum, replaceRow);
}
这导致:
warning: [unchecked] unchecked call to set(int,E) as a member of the raw type Vector
return (Vector)dataVector.set(rowNum, replaceRow);
^
where E is a type-variable:
E extends Object declared in class Vector
1 warning
答案 0 :(得分:2)
抱歉,我没有意识到DefaultTableModel正在为dataVector
使用未参数化的原始类型。我想在这种情况下,你真正做的只是对该函数的@SuppressWarnings("unchecked")
注释(这将使编译器停止抱怨),javadoc彻底,并称之为一天:
/* DOCUMENT THIS THOROUGHLY */
@SuppressWarnings("unchecked")
public Vector setRow(final int rowNum, final Vector replaceRow) {
return (Vector)dataVector.set(rowNum, replaceRow);
}
根据您的代码,看起来您真正想做的是:
Vector<Vector<Object>> dataVector = new Vector<Vector<Object>>();
public Vector<Object> setRow(final int rowNum, final Vector<Object> replaceRow) {
return dataVector.set(rowNum, replaceRow);
}
您的代码编写/设计的方式,似乎dataVector
实际上是“向量向量”,其中每个元素(一个Vector)可以包含任何类型的对象?在函数中以这种方式使用泛型,dataVector
将消除未经检查的警告。
如果我误解了,请告诉我。