Robert
Robert

Reputation: 5302

Last line of for loop executed twice?

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);
            }
    }

Upvotes: 0

Views: 1289

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533750

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;         

Upvotes: 6

Breina
Breina

Reputation: 537

Match is an object, so matches is a called a reference type. When you compare those to null, it will compare the reference to null, which it never is, so it will always return true.

If you want it to compare the contents of the object to null, you should replace matches[i] != null with matches[i].equals(null).

Upvotes: -1

Related Questions