2016-02-27 103 views
0

我想知道是否可以以某種方式實現以下僞代碼。我有4個不同大小的索引列表,因此不能使用相同的引用計數器。我需要同時運行這四個,以便可以將細節添加到一行中的datagridview。For循環,多個計數器

  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 
      } 
+2

我不你認爲你需要四個計數器。我的意思是,每當你循環時,你都會以相同的方式遞增計數器,所以你可能會用一個循環。我是否遺漏了您的實施細節? – kondrak

+0

如果我使用相同的計數器增加它們全部,我將得到一個異常,因爲一些索引的長度爲4,一些8和一些6.在我的數據網格中,如果沒有數據,我只需填充空白空間 –

+0

如果索引具有不同的最大值,for循環仍然會運行,直到** all **索引達到其最大值爲止......擁有4個或1個,您的代碼執行相同的操作。你仍然會得到一個IndexOutOfRange ...檢查你的邏輯。 –

回答

3

雖然您需要考慮如果源數組的長度不同,當範圍到達單個源數組的末尾時如何工作。

最好的辦法是有一個單一的condition是無二最長陣列,並使用三元運算?:在每列以避免訪問任何東西,如果它是列的數據範圍之外:

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] 
    ); 
} 
+0

你上面的代碼對我來說都是新的,但非常有趣。感謝您向我公開這一點。我需要閱讀更多內容以充分理解它。 –

+0

@Dai我建議將三元支票移至私人方法以避免一些冗長。最好延續良好的編碼習慣;) – kondrak

+0

@kondrak將三元變成一個函數會讓它更醜陋,因爲我們不想急於評估參數(這會導致邊界異常),也不會影響lambda調用的開銷(唯一的目前避免急切評價的方式)。 – Dai