Home > other >  Call different list with similar name in a for loop
Call different list with similar name in a for loop

Time:05-22

I have a few lists that are with names: group1, group2, group3... I need a for loop (lets say for (int i=1; i<=6; i ) and right now I check for example if i is 1 then use group1, if i is 2 use group2 etc... My question is, can I call the list with the 'i' in the end? Like groupi and when i is 1, its group1, when i is 2, its group2 etc. Here is the code:

for (int i = 0; i < groupNum; i  )
{
    if (i == 0)
    {
        foreach (Player p1 in group1)
        {
            foreach (Player p2 in group1)
            {
                if (p1 != p2)
                {
                    if (group1.IndexOf(p1) > group1.IndexOf(p2))
                    {
                        Game game = new Game(tournamentID, p1.id, p2.id);
                        game.status = "Pending";
                        gm.CreateGame(game);
                    }
                }
            }
        }
    }

    else if (i == 1)
    {
        foreach (Player p1 in group2)
        {
            foreach (Player p2 in group2)
            {
                if (p1 != p2)
                {
                    if (group2.IndexOf(p1) > group2.IndexOf(p2))
                    {
                        Game game = new Game(tournamentID, p1.id, p2.id);
                        game.status = "Pending";
                        gm.CreateGame(game);
                    }
                }
            }
        }
    }

    else if (i == 2)
    {
        foreach (Player p1 in group3)
        {
            foreach (Player p2 in group3)
            {
                if (p1 != p2)
                {
                    if (group3.IndexOf(p1) > group3.IndexOf(p2))
                    {
                        Game game = new Game(tournamentID, p1.id, p2.id);
                        game.status = "Pending";
                        gm.CreateGame(game);
                    }
                }
            }
        }
    }

    else if (i == 3)
    {
        foreach (Player p1 in group4)
        {
            foreach (Player p2 in group4)
            {
                if (p1 != p2)
                {
                    if (group4.IndexOf(p1) > group4.IndexOf(p2))
                    {
                        Game game = new Game(tournamentID, p1.id, p2.id);
                        game.status = "Pending";
                        gm.CreateGame(game);
                    }
                }
            }
        }
    }

    else if (i == 4)
    {
        foreach (Player p1 in group5)
        {
            foreach (Player p2 in group5)
            {
                if (p1 != p2)
                {
                    if (group5.IndexOf(p1) > group5.IndexOf(p2))
                    {
                        Game game = new Game(tournamentID, p1.id, p2.id);
                        game.status = "Pending";
                        gm.CreateGame(game);
                    }
                }
            }
        }
    }

    else if (i == 5)
    {
        foreach (Player p1 in group6)
        {
            foreach (Player p2 in group6)
            {
                if (p1 != p2)
                {
                    if (group6.IndexOf(p1) > group6.IndexOf(p2))
                    {
                        Game game = new Game(tournamentID, p1.id, p2.id);
                        game.status = "Pending";
                        gm.CreateGame(game);
                    }
                }
            }
        }
    }

As you can see, the code for different if statements is literally the same but the only difference is in using different lists with similar name (group1, group2..).

CodePudding user response:

create a list of grpups

 var groups = new List<Group>(){
     group1, group1, group3};

now you can do

  if(group[i].Indexof.....)

etc

CodePudding user response:

Just make an array of your groups, assuming they are List<Player> below, and iterate over that:

foreach(var grp in new List<Player>[] {group1, group2, group3, group4, group5, group6}) {
    foreach (Player p1 in grp)
    {
        foreach (Player p2 in grp)
        {
            if (p1 != p2)
            {
                if (grp.IndexOf(p1) > grp.IndexOf(p2))
                {
                    Game game = new Game(tournamentID, p1.id, p2.id);
                    game.status = "Pending";
                    gm.CreateGame(game);
                }
            }
        }
    }
}
  • Related