C# 对于循环,多个计数器

C# 对于循环,多个计数器,c#,C#,我想知道是否可以以某种方式实现以下伪代码。我有4个索引大小不同的列表,所以不能使用相同的引用计数器。我需要同时运行这四个,以便可以在一行中将详细信息添加到datagridview for (int m = 0, n = 0, p = 0, q = 0; m <= coords_Count, n <= outer_plf_Count, p <= planet_start_plf_Countt, q <= planet_plf_Count, m++,

我想知道是否可以以某种方式实现以下伪代码。我有4个索引大小不同的列表,所以不能使用相同的引用计数器。我需要同时运行这四个,以便可以在一行中将详细信息添加到datagridview

            for (int m = 0, n = 0, p = 0, q = 0; m <= coords_Count, n <= outer_plf_Count, p <= planet_start_plf_Countt, q <= planet_plf_Count, m++, n++, p++, q++)
            {
                dataGridView1.Rows.Add(
                block_coords_list[m][(int)Coords.X],        //Column 1
                block_coords_list[m][(int)Coords.Y],        //Column 2
                block_coords_list[m][(int)Coords.Z],        //Column 3
                outer_plf_list[n][(int)Outer_plf.Hash],     //Column 4
                outer_plf_list[n][(int)Outer_plf.X],        //Column 5
                outer_plf_list[n][(int)Outer_plf.Y],        //Column 6
                outer_plf_list[n][(int)Outer_plf.Z],        //Column 7
                outer_plf_list[n][(int)Outer_plf.OrbitName],        //Column 8
                outer_plf_list[n][(int)Outer_plf.PrefabName],       //Column 9
                planet_start_plf_list[p][(int)Planet_Start_plf.X],  //Column 10
                planet_start_plf_list[p][(int)Planet_Start_plf.Y],  //Column 11
                planet_start_plf_list[p][(int)Planet_Start_plf.Z],  //Column 12
                planet_start_plf_list[p][(int)Planet_Start_plf.PlanetName],  //Column 13
                planet_start_plf_list[p][(int)Planet_Start_plf.PlanetBiome], //Column 14
                planet_start_plf_list[p][(int)Planet_Start_plf.StartTrue],   //Column 15
                planet_plf_list[q][(int)Planet_plf.X],      //Column 16
                planet_plf_list[q][(int)Planet_plf.Y],      //Column 17
                planet_plf_list[q][(int)Planet_plf.Z],      //Column 18
                planet_plf_list[q][(int)Planet_plf.PlanetaryBodyName],   //Column 19
                planet_plf_list[q][(int)Planet_plf.PrefabName]);         //Column 20
            }

for(int m=0,n=0,p=0,q=0;m您可以,但是如果源阵列的长度不同,您需要考虑当范围到达单个源阵列的末尾时,它将如何工作

最好的方法是使用一个用于最长数组的
条件
,并在每列中使用三元运算符
?:
,以避免在该列的数据范围之外访问任何内容:

int a0Max = block_coords_list.Length;
int a1Max = outer_plf_list.Length;
int a2Max = planet_start_plf_list.Length;
int a3Max = planet_plf_list.Length;
int max = new Int32[] { a0Max, a1Max, a2Max, a3Max }.Max();

for (int i = 0; i < max, i++)
{
    dataGridView1.Rows.Add(
        i < a0Max ? block_coords_list[i][(int)coords.X] : null,
        i < a0Max ? block_coords_list[i][(int)coords.Y] : null,
        i < a0Max ? block_coords_list[i][(int)coords.Z] : null, 
        i < a1Max ? outer_plf_list[i][(int)outer_plf.Hash] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.X] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.Y] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.Z] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.OrbitName] : null,
        i < a1Max ? outer_plf_list[i[(int)outer_plf.PrefabName] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.X] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.Y] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.Z] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.PlanetName] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.PlanetBiome] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.StartTrue] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.X] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.Y] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.Z] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.PlanetaryBodyName] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.PrefabName]
    );
}
int a0Max=block\u coords\u list.Length;
int a1Max=外部plf列表长度;
int a2Max=行星的起始位置列表长度;
int a3Max=行星的长度;
int max=new Int32[]{a0Max,a1Max,a2Max,a3Max}.max();
对于(int i=0;i
您可以,但如果源阵列的长度不同,您需要考虑当范围到达单个源阵列的末尾时,该范围的工作方式

最好的方法是使用一个用于最长数组的
条件
,并在每列中使用三元运算符
?:
,以避免在该列的数据范围之外访问任何内容:

int a0Max = block_coords_list.Length;
int a1Max = outer_plf_list.Length;
int a2Max = planet_start_plf_list.Length;
int a3Max = planet_plf_list.Length;
int max = new Int32[] { a0Max, a1Max, a2Max, a3Max }.Max();

for (int i = 0; i < max, i++)
{
    dataGridView1.Rows.Add(
        i < a0Max ? block_coords_list[i][(int)coords.X] : null,
        i < a0Max ? block_coords_list[i][(int)coords.Y] : null,
        i < a0Max ? block_coords_list[i][(int)coords.Z] : null, 
        i < a1Max ? outer_plf_list[i][(int)outer_plf.Hash] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.X] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.Y] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.Z] : null,
        i < a1Max ? outer_plf_list[i][(int)outer_plf.OrbitName] : null,
        i < a1Max ? outer_plf_list[i[(int)outer_plf.PrefabName] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.X] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.Y] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.Z] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.PlanetName] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.PlanetBiome] : null,
        i < a2Max ? planet_start_plf_list[i][(int)planet_Start_plf.StartTrue] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.X] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.Y] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.Z] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.PlanetaryBodyName] : null,
        i < a3Max ? planet_plf_list[i][(int)planet_plf.PrefabName]
    );
}
int a0Max=block\u coords\u list.Length;
int a1Max=外部plf列表长度;
int a2Max=行星的起始位置列表长度;
int a3Max=行星的长度;
int max=new Int32[]{a0Max,a1Max,a2Max,a3Max}.max();
对于(int i=0;i
我认为您不需要四个计数器。我的意思是,每次循环时,您都以相同的方式递增计数器,因此您可能只需要一个。我是否遗漏了实现的一些细节?如果我使用相同的计数器递增计数器,我将得到一个异常,因为有些索引的长度为4,有些索引的长度为18和一些6.在我的datagrid中,如果没有数据,我就用空格填充它们。如果索引有不同的最大值,for循环仍将运行,直到所有索引都达到最大值…有4个或有1个,你的代码也会这样做。你仍然会得到一个IndexOutfrance…检查你的逻辑。而不是不同的计数器(不起作用),使用Math.Min(计数器,x)式中,x表示可接受的最大值item@Gusman我对此并不熟悉,但我会去研究一下。我认为你不需要四个计数器。我的意思是,你每次循环都以相同的方式递增计数器,所以你可能只需要一个。我是否遗漏了你实现的一些细节?如果我递增它们都使用相同的计数器,我会得到一个异常,因为有些索引的长度为4,有些索引的长度为8,有些索引的长度为6。在我的datagrid中,如果没有数据,我就用空格填充它们。如果索引有不同的最大值,for循环仍将运行,直到所有索引都达到t