我正在延长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
实际上是一个“vector 的 vector ”,其中每个元素( vector )可以容纳任何类型的对象?在您的函数中以这种方式使用泛型和 dataVector
将消除未经检查的警告。
如果我理解有误,请告诉我。
关于Java 调用 Vector.set() 导致 'unchecked' 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207592/