我想使用递归方法获取 XML 文件的最大深度,首先我已经声明了变量
public static int maxdepth=0;
private static void GetDepth(NodeList nl, int level,int maxdepth) {
level++;
if (maxdepth<level)
{
maxdepth= level;
}
if(nl != null && nl.getLength() > 0){
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n instanceof Element)
{
GetDepth(n.getChildNodes(), level, maxdepth);
}
}
}
}
public static void main(String[] args) {
NodeList nl = root.getChildNodes();
GetDepth(nl,level,maxdepth);
System.out.println(maxdepth);
}
当我显示变量 maxdepth 的值时,我收到值 0,作为声明
最佳答案
getDepth
方法签名中的int maxdepth
隐藏了静态变量maxdepth
。从签名中删除它:
private static void GetDepth(NodeList nl, int level)
那么这个方法就可以工作了。
关于java - 递归方法最大深度-静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336355/