我正在扩展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
抱歉,我没有意识到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
实际上是一个“向量的向量”,其中每个元素(向量)可以容纳任何种类的对象?在函数中以这种方式使用泛型,dataVector
将消除未经检查的警告。
如果我误解了,请告诉我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句