Java 为什么eclipse说即使使用了局部变量,也不使用它?
我有以下代码Java 为什么eclipse说即使使用了局部变量,也不使用它?,java,eclipse,Java,Eclipse,我有以下代码 private List<String> getItems() { XmlDoc document = new XmlDoc(); List<String> itemList = new ArrayList<String>(); String itemNum; try { XmlUtils root = document.parse(xmlFile); List<XmlUtil
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;
}
private List getItems(){
XmlDoc document=新的XmlDoc();
List itemList=new ArrayList();
字符串itemNum;
试一试{
XmlUtils root=document.parse(xmlFile);
List listNode=root.getChildNodes();
for(XmlUtils节点:listNode){
itemNum=node.getValue();
}
}捕获(XMLE异常){
e、 printStackTrace();
}
返回项目列表;
}
尽管我在for循环中使用了字符串itemNum,但eclipse表示“未使用局部变量itemNum的值”。为什么会这样?因为您正在为
itemNum
赋值,但从未读取它。这是变量未使用的明显迹象。事实上,您可以安全地删除此行:
itemNum = node.getValue();
…什么也不会发生。也许你应该在分配完它之后用
itemNum
做点什么。你在哪里使用它?你认为赋值有什么影响?我在String itemNum中看到它;给一个变量赋值不是“使用”它吗?@user1629109不,你只是在里面放了一个值,但是你希望以后用这个值做点什么,如果不是,那有什么用呢?我在列表中添加了这个Sting,警告信息消失了。我只是好奇而已。谢谢lot@user1629109完美的这是我的荣幸:)