首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环中的循环迭代6次,但打印9次而不是3次。

循环中的循环迭代6次,但打印9次而不是3次。
EN

Stack Overflow用户
提问于 2015-03-24 21:11:43
回答 2查看 85关注 0票数 0

我使用ant替换url中的值:

代码语言:javascript
复制
<property name="dir" value="dir1, dir2, dir3" />
<property name="files" values="file1, file2, file3" />

我希望将url中的值替换为dir1、file1然后是dir2、file2然后是dir3、file3。我循环两次来替换,但不是打印三次和替换所有的值,它替换和打印6次。

这里是我的代码:

代码语言:javascript
复制
<target name="test">
<foreach param="dirval" in="${dir}">
<foreach param="filesval" in="${files}">
<sequential>
<echo message="Testing structure: ${dirval}/${filesval}" />
</sequential>
</foreach>
</foreach>

预期输出:

代码语言:javascript
复制
Testing structure: dir1/file1
Testing structure: dir2/file2
Testing structure: dir3/file3

但got:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2015-03-24 21:22:22

之所以有此输出,是因为您处于一个由3个元素组成的双foreach循环中,因此您可以循环并打印结果9次,而不是所需的3次。( dir上的foreach循环,文件上的3次*foreach循环,3次)(正如您在当前输出中看到的那样)

我不知道Ant,但在Java中,您要完成的任务如下所示。(以获得所需的结构或输出)

只使用一个循环:

代码语言:javascript
复制
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])
}
票数 1
EN

Stack Overflow用户

发布于 2015-03-25 19:00:38

下面的代码使用第三方蚂蚁-对照库。Ant-cont肋骨提供了几个允许我们模拟数组的任务:

代码语言:javascript
复制
<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>

结果:

代码语言:javascript
复制
run:
     [echo] Testing structure: dir1/file1
     [echo] Testing structure: dir2/file2
     [echo] Testing structure: dir3/file3
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29242976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档