我有以下代码
private List<String> getItems() {
XmlDoc document = new XmlDoc();
List<String> itemList = new ArrayList<String>();
String itemNum;
try {
XmlUtils root = document.parse(xmlFile);
List<XmlUtils> listNode = root.getChildNodes();
for (XmlUtils node : listNode) {
itemNum = node.getValue();
}
} catch (XmlException e) {
e.printStackTrace();
}
return itemList;
}
即使我在 for 循环中使用了 String itemNum eclipse 也会说“未使用局部变量 itemNum 的值”。为什么会这样?
最佳答案
因为您正在为 itemNum
分配一个值,但从未读取它。这是该变量未被使用的明显标志。事实上,您可以安全地删除这一行:
itemNum = node.getValue();
……什么也不会发生。也许您实际上应该在分配 itemNum
后做一些事情。
关于java - 为什么eclipse虽然使用了局部变量却说未使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29434813/