for循环的最后一行执行两次? [英] Last line of for loop executed twice?

查看:365
本文介绍了for循环的最后一行执行两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Java初学者,这是我的第一篇文章。
我找不到任何完全像我的问题,虽然这个帖子看起来类似:
为什么这个print line命令执行两次?



但是答案并没有帮我解决它。

>

我知道这可能是一些愚蠢的事情,但希望你们中的一个人能够指出为什么名为matches的数组中的最后一项输出两次。 p>

预先感谢,
Robert。



这是我的代码:

  public String buildMatchList(Match [] matches)
{
fixtures =;

int i = 0;
for(i = 0; i< numMatches; i ++)
{
if(matches [i]!= null)
{
fixtures + = String。格式(\\\
%-10.10s%10.9s%15.14s,匹配[i] .getTeamA(),Vs,匹配[i] .getTeamB());
}
}
System.out.println(fixtures);
}

// -EDIT -
//在此方法中设置的numMatches

public void fillMatchArray(Team [] sortedTeams,int numTeams)
{
int homeTeam = 0;
int awayTeam = 0;
目标A = 0;
goalsB = 0;
fixtures =;
布尔播放=假;
matches = new Match [MAX_NUM_GAMES]; (homeTeam = 0; homeTeam< sortedTeams.length; homeTeam ++)
(awayTeam = homeTeam + 1; awayTeam< sortedTeams.length; awayTeam ++)

) teamA = sortedTeams [homeTeam] .getTeamName();
字符串teamB = sortedTeams [awayTeam] .getTeamName();
matchFixtures =新赛果(teamA,teamB,goalsA,goalsB,出场);
{
fixtures + = String.format(\\\
%-10.10s%10.9s%15.14s,
matchFixtures.getTeamA(),Vs,matchFixtures.getTeamB ));
}
int i = 0;
匹配[i] = matchFixtures;
numMatches ++;
buildMatchList(matches);



$ div $解析方案

如果它打印出两次,最可能的解释是最后两个条目是相同的。有一个常见的错误,你添加一个可变对象的集合两次,而你认为​​他们是不同的,他们不是。



我建议你尝试通过代码在你的调试器中看看它在做什么?




这是通过代码的步骤会有帮助的。你每次都设置数组的第一个元素,因为我总是0

  int i = 0; 
匹配[i] = matchFixtures;
numMatches ++;

更改为

 匹配[numMatches ++] = matchFixtures; 


I'm a Java beginner and this is my first post. I couldn't find anything exactly like my problem although this post seemed similar: Why is this print line command executing twice?

but the answers didn't help me solve it.

I know it's probably something stupid but was hoping one of you folks might be able to point out to me why the last entry in the array named "matches" prints out twice.

Thanks in advance, Robert.

Here is my code:

public String buildMatchList(Match[] matches)
{   
        fixtures = "";

        int i = 0;
        for ( i = 0; i < numMatches; i++)
        {
            if (matches[i] != null)
            {
                fixtures += String.format("\n%-10.10s %10.9s %15.14s", matches[i].getTeamA(), " Vs ", matches[i].getTeamB());           
            }
        }
        System.out.println(fixtures);
}

// -EDIT -
// numMatches set in this method

public void fillMatchArray(Team[] sortedTeams, int numTeams)
    {
        int homeTeam = 0;
        int awayTeam = 0;
        goalsA = 0;
        goalsB = 0;
        fixtures = "";
        boolean played = false;
        matches = new Match[MAX_NUM_GAMES];
        for (homeTeam = 0; homeTeam < sortedTeams.length; homeTeam++)
            for (awayTeam =  homeTeam+1; awayTeam < sortedTeams.length; awayTeam++ )
            {
                String teamA = sortedTeams[homeTeam].getTeamName();
                String teamB = sortedTeams[awayTeam].getTeamName();             
                matchFixtures = new Match(teamA, teamB, goalsA, goalsB, played);
                {
                    fixtures += String.format("\n%-10.10s %10.9s %15.14s", 
                            matchFixtures.getTeamA(), " Vs ", matchFixtures.getTeamB());    
                }               
                int i = 0;          
                matches[i] = matchFixtures;         
                numMatches++;           
                buildMatchList(matches);
            }
    }

解决方案

If it prints out twice, the most likely explanation is that the last two entries are the same. There is a common bug where you add a mutable objects to a collection twice and while you think they are different, they are not.

I suggest you try stepping through the code in your debugger to see what it doing?


this is where stepping through the code would be helpful. You are setting the first element of the array each time as i is always 0

            int i = 0;          
            matches[i] = matchFixtures;         
            numMatches++; 

change it to

matches[numMatches++] = matchFixtures;         

这篇关于for循环的最后一行执行两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆