2010-03-02 70 views
0

如何按顺序中断分组排序的数字集合?按顺序中断分组排序的数字集合

例如:

List<int> someList = new List<int>{1,2,3,7,8,9} 

输出:

someDictionary[0].Key == 1; 
someDictionary[0].Value == 3; 

someDictionary[1].Key == 7; 
someDictionary[1].Value == 9; 
+2

这可能是我,但我有不知道你在问什么,澄清一点? – 2010-03-02 14:53:48

+0

如何将已排序的数字列表拆分为数字将连续的其他列表。例如。 list1 - 1,2,3,list2 - 7,8,9等。 – Jacob 2010-03-02 15:03:19

+0

仍然没有得到它。你能否编辑这个问题需要发生的变革的详细信息? – 2010-03-02 15:11:46

回答

2

如果我明白你正确地这样的事情可以做的伎俩

private static List<List<int>> splitBySeq(List<int> someList) 
    { 
     //Stores the result 
     List<List<int>> result = new List<List<int>>(); 

     //Stores the current sequence 
     List<int> currentLst = new List<int>(); 
     int? lastNumber = null; 

     //Iterate the items 
     for (int i = 0; i < someList.Count; i++) 
     { 
      //If the have a "break" in the sequence and this isnt the first item 
      if (lastNumber != null && someList[i] != lastNumber + 1) 
      { 
       result.Add(currentLst); 
       currentLst = new List<int>(); 
      } 

      currentLst.Add(someList[i]); 
      lastNumber = someList[i]; 
     } 

     if (currentLst.Count != 0) 
      result.Add(currentLst); 

     return result; 
    }