【问题标题】:Loop within a loop iterates 6 times but prints 9 times instead of 3循环内循环迭代 6 次,但打印 9 次而不是 3 次
【发布时间】:2025-11-08 09:00:01
【问题描述】:

我正在使用 ant 替换 url 中的值:

<property name="dir" value="dir1, dir2, dir3" />
<property name="files" values="file1, file2, file3" />

我想替换 url 中的值,例如 dir1、file1 然后 dir2、file2 然后 dir3、file3。我循环两次替换,但不是打印三次并替换所有值,而是替换和打印 6 次。

这是我的代码:

<target name="test">
<foreach param="dirval" in="${dir}">
<foreach param="filesval" in="${files}">
<sequential>
<echo message="Testing structure: ${dirval}/${filesval}" />
</sequential>
</foreach>
</foreach>

预期输出:

Testing structure: dir1/file1
Testing structure: dir2/file2
Testing structure: dir3/file3

但是得到了:

Testing structure: dir1/file1
Testing structure: dir1/file2
Testing structure: dir1/file3    
Testing structure: dir2/file1
Testing structure: dir2/file2
Testing structure: dir2/file3
Testing structure: dir3/file1
Testing structure: dir3/file2
Testing structure: dir3/file3

【问题讨论】:

    标签: java ant ant-contrib


    【解决方案1】:

    您有此输出的原因是您处于每个 3 个元素的双 foreach 循环中,因此您循环并打印结果 9 次而不是所需的 3 次。 (foreach 循环遍历 dir,3 次 * foreach 循环遍历文件,3 次)(正如您在当前输出中看到的那样)

    我不了解 Ant,但在 Java 中,您要完成的工作看起来像这样。 (得到想要的结构,或者输出)

    只使用一个循环:

    string dir[] = {"dir1","dir2","dir3"};
    string files[] = {"file1","file2","file3"};
    
    for (int i = 0; i < dir.length, i++){
        System.out.println("Testing structure: " + dir[i] + "/" + file[i])
    }
    

    【讨论】:

    • 嗯,我知道如何用 Java 做,但我在这里寻找蚂蚁专家,因为他们可能知道问题并且知道得更好
    【解决方案2】:

    以下代码使用第三方Ant-Contrib library。 Ant-Contrib 提供了几个允许我们模拟数组的任务:

    <project name="ant-foreach-array" default="run">
        <!-- Ant-Contrib provides <var>, <for>, <math>, and <propertycopy>. -->
        <taskdef resource="net/sf/antcontrib/antlib.xml" />
    
        <target name="run">
            <property name="dirs" value="dir1,dir2,dir3" />
            <property name="files" value="file1,file2,file3" />
    
            <var name="fileIndex" value="1" />
            <for list="${files}" delimiter="," param="file">
                <sequential>
                    <property name="file.${fileIndex}" value="@{file}" />
                    <math 
                        result="fileIndex" datatype="int"
                        operand1="${fileIndex}" operation="+" operand2="1" 
                    />
                </sequential>
            </for>
    
            <var name="dirIndex" value="1" />
            <for list="${dirs}" delimiter="," param="dir">
                <sequential>
                    <propertycopy 
                        name="fileIndex"
                        from="file.${dirIndex}"
                        override="true"
                    />
                    <echo message="Testing structure: @{dir}/${fileIndex}" />
                    <math 
                        result="dirIndex" datatype="int"
                        operand1="${dirIndex}" operation="+" operand2="1"
                    />
                </sequential>
            </for>
        </target>
    </project>
    

    结果:

    run:
         [echo] Testing structure: dir1/file1
         [echo] Testing structure: dir2/file2
         [echo] Testing structure: dir3/file3
    

    【讨论】: