[英]Java: why isn't the updated value being returned?
public static int getElementIdx (DOMElement elt) {
int count = 1;
for (DOMElement sib = (DOMElement) elt.getPreviousSibling();
sib != null;
sib = (DOMElement) sib.getPreviousSibling())
{
System.out.println("sib "
+ sib.getTagName () + " elt " + elt.getTagName ());
if (sib.ELEMENT_NODE == sib.getNodeType () &&
sib.getTagName () == elt.getTagName ()) {
System.out.println (count);
count++;
}
}
return count;
}
count always returns 1. However, inside the for loop, it returns the incremented count value. count始终返回1。但是,在for循环内,它将返回递增的count值。 This is really strange, I thought declaring a local variable count outside of the for loop should work.... 这真的很奇怪,我认为在for循环之外声明一个局部变量计数应该起作用。
The count
usage is fine--the inner if statement is likely never true. count
用法很好-内部if语句可能永远不会为真。
The culprit is likely to be 罪魁祸首很可能是
sib.getTagName() == elt.getTagName()
You need to check String equality using equals()
: 您需要使用equals()
检查字符串是否相等:
sib.getTagName().equals(elt.getTagName())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.