Java For循环计数器在调用递归方法时不工作

标签 java loops recursion

我正在尝试遍历对象列表并将其属性打印到 xml 文件,但由于需要关闭标记,我需要递归遍历子对象并打印其属性,然后再返回顶部堆栈的顶部写出最高组件关闭标记。

然而,for循环和递归在java中似乎不能很好地协同工作,因为一些奇怪的原因,当我在for循环内部有一个方法调用自身时,计数器“重置”(本质上计数器变量似乎是当尝试正常使用带有所有 3 个参数的 for 循环时取消分配自身),当进入循环的下一次迭代时,导致无限循环。

我尝试了多种不同的方法,包括尝试在类构造函数中将计数器设置为静态变量,并在 for-each 循环中进行替换,但都存在类似的问题。我找到的最接近答案是以下解决方案,将计数器传入并返回到递归方法中:

Adding counter to a loop inside a recursive method - Java

然而,虽然这适用于上面的情况,即仅添加一个值,但它会导致问题,因为如果我不调整计数器,它现在比需要的值高一个,并导致 indexOutOfBound 异常,如果我在进入循环之前减去计数器和/或将其分配给 0,则即使我在将计数器传回之前显式地向计数器添加 1,返回也会返回 0。

这些都没有任何意义。这是我正在使用的代码的基本逻辑:如果有人知道这有什么问题,或者知道任何替代解决方案,请告诉我。

public int write(PrintWriter fromPortalTXTFile, String level, Integer counter){
    ...
    if (children) {
        for(;counter < childrenList.size();) {
            counter++;
            counter = ClassName.get(counter - 1).write(fromPortalTXTFile, level, counter);
        }
    }
    else {
        counter++;
    }

    return counter;
}

编辑:这是由于各种请求而产生的完整方法代码。它包含对不同函数的引用,并且具有与我遇到的问题无关的逻辑,并且我无法详细说明该方法的每个部分是如何工作的。问题出在组件有子组件时进入的 for 循环,我仍然相信上面已经更好地说明了这一点。

// Writes the information about this BOM component out to the from_portal.txt file
public int write(PrintWriter fromPortalTXTFile, String level, Integer counter) throws Exception {

    //Retrieve the item and revision of the BOM line.  If
    //read access is denied, skip the BOM line.
    Debug.println("PERF: Inside printXMLTag: Reading BOMLine props start");        
    TCComponentItem item = TXDExportAction.getItem(currentComponent);
    TCComponentItemRevision rev = TXDExportAction.getItemRevision(currentComponent);
    //if (item == null || rev == null) {
    //    return null;
    //}
    String itemID = item.getProperty("item_id");
    String revID = rev.getProperty("item_revision_id");

    // Get the pdm_occ_id of the current component
    String pdmOccID = TXDExportAction.getBOMLineProperty(currentComponent, "bl_occurrence_uid");

    // Determine if the item is currently selected in the BOM window
    Boolean isSelected = false;

    //if (selectedComponents.contains(currentComponent)){
    //  isSelected = true;
    //}

    // TODO: See if it is actually needed to get the quantity of packed lines

    //If the user created a single occurrence to represent
    //multiple occurrences, get the quantity
    int n = 1;
    boolean packed = currentComponent.isPacked();
    if (!packed) {
        try {
            //String str = icbl.getProperty("bl_quantity");
            String str = TXDExportAction.getBOMLineProperty(currentComponent, "bl_quantity");
            if (str != null) {
                n = Integer.parseInt(str);
            }
        } catch (NumberFormatException e) {
            //Do nothing
        }
    }

    Debug.println("PERF: Inside printXMLTag: Reading BOMLine props  complete");

    //TODO: See why this is in a loop, and if it is necessary
    XMLStringBuffer buf = new XMLStringBuffer();

    //Loop over the BOM line n times
    for (int count = 0; count < n ; count++) {
        //Build the opening XML entry
        //XMLStringBuffer buf = new XMLStringBuffer();
        buf.startTag(TXDExportAction.BOMLINE);
        buf.appendAttribute(TXDExportAction.ITEM_ID, itemID);
        buf.appendAttribute(TXDExportAction.REV_ID, revID);

        buf.appendAttribute(TXDExportAction.PDM_OCC_ID, pdmOccID);
        buf.appendAttribute(TXDExportAction.ITEM_SELECTED, isSelected);

        // If this is not the lowest level tag, don't put in the slash at the end.
        // If it is, close the tag.
        //TODO: Remove writing of tags, add to the BOMElement.write function.
        //if (closureTags == 0){
        //  buf.endTagBracket();
        //}
        //else{
        //  buf.endTag();

        //  //Handle closure tags for parents
        //    for(int i = 0; i < closureTags; i++){
        //      buf.endTag(BOMLINE);
        //    }
        //}


    }
    buf.endTagBracket();

    // Step 3: write a </bomline> tag at the same level
    fromPortalTXTFile.print(level);
    fromPortalTXTFile.println(buf);
    //buf.endTag(TXDExportAction.BOMLINE);

    if (!childBOMElements.isEmpty()){
        // Step 1: print tag + information
        level = level + "    ";
        // Step 2: for each child, call child.write()
        for (; counter < childBOMElements.size();){
            counter ++;
            //BOMElement nextElement = childBOMElements.get(index);
            counter = childBOMElements.get(counter - 1).write(fromPortalTXTFile, level, counter); 
        }
        // </Bomline> tag
        //fromPortalTXTFile.println(buf.endTag(TXDExportAction.BOMLINE));
    }
    else
    {
        // If no children, close the tag with a leaflet
        //fromPortalTXTFile.println(buf);
        counter++;
        fromPortalTXTFile.print(buf.endTag());
    }


    // For some reason, using a recursive function in a for loop resets the counter of the for loop.
    // The only work-around is to pass the counter back as a return method.
    return counter;
}

最佳答案

这是因为您在调用递归方法之前增加了 counter (这就是为什么您在递归调用中需要 counter - 1 并且完全多余)。

也就是说,您遇到的问题是您的总体构造。您传入计数器并定义每个计数器值的循环。但是,不能保证子对象拥有其父对象的子对象数量。父级 A 有 3 个子级,因此您要传入值为 0、1 或 2 的 counter。但是,如果子级 A 本身只有 1 个子级,则将使用 counter > 1 或 2 将导致其失败(出现 IndexOutOfBounds 错误)。

public void write(PrintWriter fromPortalTXTFile, String level) {

    . . . 

    if(children) {
        for(int n = 0; n < childrenList.size(); n++) {
            // you need to pass in the child object, unless 
            // you're controlling it with level
            write(childrenList.get(n), level);
        }
    } else {

        . . .

    }
}

请记住,这不会是完美的。我无权访问您的整个代码。不知道什么水平在做。我不知道你是如何获得childrenList的。

但是你把事情过于复杂化了。如果存在子项,您所需要做的就是对每个子项进行递归。

编辑:来自评论链:

您可能没有正确处理根文档的子文档。要么是这样,要么是无限循环,因为它会永远重新启动 for 循环(因此 n 始终为 0)。您可以通过查看级别是否正确递增(或递减)来测试这一点。

因此,上面的例子并不是完整的答案。解决如何处理子对象是解决方案的一部分。

关于Java For循环计数器在调用递归方法时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22301900/

相关文章:

scala - 如何优化 Scala 递归函数的双重调用

java - 递归 - 添加数组的子集

javascript - 如何计算该问答流程的最短和最长路径?

java - 将 Java 应用程序部署到 GAE

java - 向 JPanel 添加仅调用 onload 的监听器

c - 代码中的错误。我不知道它属于什么类别

javascript - 如何在循环中为 jQuery 函数的变量设置动画?

java - 找不到 OSGi 包的 Activator 类

java - 确定复杂度等级

java - while 循环会停止执行吗?