2017-04-18 89 views
0

我有串的下面列出將多個列表垂直成一個字符串列表

List<string> List1 = new List<string> { "P1", "P2", "P3" }; 
List<string> List2 = new List<string> { "Q1", "Q2", "Q3" }; 
List<string> List3 = new List<string> { "R1", "R2", "R3" }; 

//........ 
// Add List1,List2, List3 values Vertically to CombileList 

CombineList = { "P1", "Q1", "R1", "P2", "Q2", "R2", "P3", "Q3", "R3" }; 

我要值到CombineList從所有列表垂直添加,如CombineList所示,可以列出的n個以相同的方式添加到CombineList。

+2

你有試過什麼嗎?使用循環這不應該太難。 – Ivar

+0

這個答案可能是你正在尋找的:http://stackoverflow.com/a/10298725/390819 – GolfWolf

+0

http://stackoverflow.com/questions/40768322/merge-multiple-lists-with-variable-length-popping每個元素 –

回答

0

如果名單是相同的大小,你可以使用一個for循環:

List<string> list1 = new List<string> { "P1", "P2", "P3" }; 
List<string> list2 = new List<string> { "Q1", "Q2", "Q3" }; 
List<string> list3 = new List<string> { "R1", "R2", "R3" }; 

List<string> combinedList = new List<string>(); 

for(int i = 0; i < list1.Count; i++) 
{ 
    combinedList.Add(list1[i]); 
    combinedList.Add(list2[i]); 
    combinedList.Add(list3[i]); 
} 
0

類似的問題問Here

使用IEnumeratorMoveNext()方法,你可以循環數組上,並結合他們如何你喜歡

0

通過使用枚舉:

public List<T> CombineVertically<T>(List<List<T>> Source) 
     { 
      List<T> result = new List<T>(); 

      var enumerators = Source.Select(x => x.GetEnumerator()); 
      while (enumerators.Where(x => x.MoveNext()).Count() > 0)    
       result.AddRange(enumerators.Select(x => x.Current)); 

      enumerators.ToList() 
       .ForEach(x => x.Dispose()); 

      return result; 
     } 
+1

'IEnumerator '實現'IDisposable',不要忘記'Dispose'所有的實例 –

+0

@DmitryBychenko你完全正確 – 2017-04-18 08:28:15

相關問題