类型安全:类型 List 的表达式需要未经检查的转换以符合 Collection<?扩展对象>
Type safety: The expression of type List needs unchecked conversion to conform to Collection<? extends Object>
我正在使用 set 为某些操作定义允许的键。 Eclipse 显示此警告:
Type safety: The expression of type List needs unchecked conversion
to conform to Collection<? extends Object>
我用谷歌搜索了一下,在略有不同的情况下发现了相同的消息,但它可能是类似的问题。
有没有机会以其他方式摆脱这个警告
@SuppressWarnings("unchecked")
使用
是个好主意
@SuppressWarnings("unchecked")
在这种情况下?
这是我的代码:
public static final String KEY_A = "A_VALUE";
public static final String KEY_B = "B_VALUE";
public static final Set<?> allowedKeys = new HashSet<Object>(Arrays.asList(new String[] {KEY_A, KEY_B}));
Set<?>
或 Set<Object>
仅应在您必须使其与现有代码一起使用的情况下使用。否则,尝试制作特定类型。
如果您确定该集合仅包含 String
个元素,则 Set<String>
是最佳选择。
Eclipse 搞得一团糟:
错误:
import edu.emory.mathcs.backport.java.util.Arrays;
正确:
import jave.util.Arrays;
所以代码在 Set<?>
和 Set<String>
两个版本都可以。 Eclipse 刚刚自动导入错误 class.
尝试使用源代码了解问题并消除类型未经检查的转换警告,
private static Vector<Vector<String>> tableVectorData;
private static Vector<String> rowData = new Vector<String>();
用于添加元素 -
rowData.clear();
rowData.addElement((strings[0])[0][0]);
rowData.addElement((strings[0])[1][0]);
tableVectorData.addElement(rowData);
用于检索元素 -
model = (DefaultTableModel) table.getModel();
rowCount = tableVectorData.size();
int i = 0;
while(i < rowCount) {
Vector<String> row = tableVectorData.get(i++);//here the type check warning will occur
model.setRowCount(model.getRowCount()+1);
System.out.println(row);
model.setValueAt(row.get(0), model.getRowCount()-1, 0);
model.setValueAt(row.get(1), model.getRowCount()-1, 1);
}
或使用Iterator<Vector<String>>
-
Iterator<Vector<String>> rows = tableVectorData.iterator();//here the type check warning will occur
boolean flag = false;
checkValue:
while(rows.hasNext()) {
Vector<String> vect = rows.next();
if(vect.contains(value)) {
flag = true;
break checkValue;
}
}
希望这些能为您提供处理这些表达式类型检查警告的方法,谢谢。
我正在使用 set 为某些操作定义允许的键。 Eclipse 显示此警告:
Type safety: The expression of type List needs unchecked conversion
to conform to Collection<? extends Object>
我用谷歌搜索了一下,在略有不同的情况下发现了相同的消息,但它可能是类似的问题。
有没有机会以其他方式摆脱这个警告
@SuppressWarnings("unchecked")
使用
是个好主意@SuppressWarnings("unchecked")
在这种情况下?
这是我的代码:
public static final String KEY_A = "A_VALUE";
public static final String KEY_B = "B_VALUE";
public static final Set<?> allowedKeys = new HashSet<Object>(Arrays.asList(new String[] {KEY_A, KEY_B}));
Set<?>
或 Set<Object>
仅应在您必须使其与现有代码一起使用的情况下使用。否则,尝试制作特定类型。
如果您确定该集合仅包含 String
个元素,则 Set<String>
是最佳选择。
Eclipse 搞得一团糟:
错误:
import edu.emory.mathcs.backport.java.util.Arrays;
正确:
import jave.util.Arrays;
所以代码在 Set<?>
和 Set<String>
两个版本都可以。 Eclipse 刚刚自动导入错误 class.
尝试使用源代码了解问题并消除类型未经检查的转换警告,
private static Vector<Vector<String>> tableVectorData;
private static Vector<String> rowData = new Vector<String>();
用于添加元素 -
rowData.clear();
rowData.addElement((strings[0])[0][0]);
rowData.addElement((strings[0])[1][0]);
tableVectorData.addElement(rowData);
用于检索元素 -
model = (DefaultTableModel) table.getModel();
rowCount = tableVectorData.size();
int i = 0;
while(i < rowCount) {
Vector<String> row = tableVectorData.get(i++);//here the type check warning will occur
model.setRowCount(model.getRowCount()+1);
System.out.println(row);
model.setValueAt(row.get(0), model.getRowCount()-1, 0);
model.setValueAt(row.get(1), model.getRowCount()-1, 1);
}
或使用Iterator<Vector<String>>
-
Iterator<Vector<String>> rows = tableVectorData.iterator();//here the type check warning will occur
boolean flag = false;
checkValue:
while(rows.hasNext()) {
Vector<String> vect = rows.next();
if(vect.contains(value)) {
flag = true;
break checkValue;
}
}
希望这些能为您提供处理这些表达式类型检查警告的方法,谢谢。