2011-09-03 97 views
0

理想情況下,我想添加到NumberList,而不刪除它製作另一個NumberList,並將其添加到NumberRanges,List。如何將添加到列表集合添加到另一個列表集合中?

//How to add 8 to NumberRanges.NumberGroup.GroupEven 
//How to add 9 to NumberRanges.NumberGroup.GroupOdd 
AddEvenNumberToRange(oNumberRanges, 8); 
AddOddNumberToRange(oNumberRanges, 9); 


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Collections; 

namespace AddToList 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      NumberList oGroupEven = new NumberList(); 
      InitialLoadEvenNumbers(oGroupEven); 
      NumberList oGroupOdd = new NumberList(); 
      InitialLoadOddNumbers(oGroupOdd); 

      NumberRanges oNumberRanges = new NumberRanges(); 
      CombineNumberRanges(oGroupEven, oGroupOdd, oNumberRanges); 
      OutPutRanges(oNumberRanges); 
      Console.ReadKey(); 

      //This code below will throw execption unitl implemented. 
      //How to add 8 to NumberRanges.NumberGroup.GroupEven 
      //How to add 9 to NumberRanges.NumberGroup.GroupOdd 
      AddEvenNumberToRange(oNumberRanges, 8); 
      AddOddNumberToRange(oNumberRanges, 9); 
      OutPutRanges(oNumberRanges); 

      Console.ReadKey(); 
     } 

     private static void AddOddNumberToRange(NumberRanges oNumberRanges, int p) 
     { 
      //I'm stuck on the best approach to add a number to existing collection 
      throw new NotImplementedException(); 
     } 

     private static void AddEvenNumberToRange(NumberRanges oNumberRanges, int p) 
     { 
      //I'm stuck on the best approach to add a number to existing collection 
      throw new NotImplementedException(); 
     } 


     private static void OutPutRanges(NumberRanges oNumberRanges) 
     { 
      foreach (NumberList olist in oNumberRanges) 
      { 
       foreach (int iNum in olist) 
       { 
        Console.WriteLine(iNum.ToString()); 
       } 
      } 
     } 

     private static void CombineNumberRanges(NumberList oGroupEven, NumberList oGroupOdd, NumberRanges oNumberRanges) 
     { 
      oNumberRanges.NumberGroups.Add(oGroupEven); 
      oNumberRanges.NumberGroups.Add(oGroupOdd); 
     } 

     private static void InitialLoadOddNumbers(NumberList oGroupOdd) 
     { 
      oGroupOdd.Number.Add(3); 
      oGroupOdd.Number.Add(5); 
      oGroupOdd.Number.Add(7); 
     } 

     private static void InitialLoadEvenNumbers(NumberList oGroupEven) 
     { 
      oGroupEven.Number.Add(2); 
      oGroupEven.Number.Add(4); 
      oGroupEven.Number.Add(6); 
     } 
    } 

    public class NumberList : IEnumerator, IEnumerable 
    { 

     public List<int> Number = new List<int>(); 

     #region GetEnumeratorSetup 
     private int m_iPosition = -1; 

     public IEnumerator GetEnumerator() 
     { 
      return Number.GetEnumerator(); 
     } 

     public object Current 
     { 
      get 
      { 
       return Number; 
      } 
     } 

     public bool MoveNext() 
     { 
      m_iPosition++; 
      return true; 
     } 

     public void Reset() 
     { 
      m_iPosition = 0; 
     } 
     #endregion 
    } 

    public class NumberRanges: IEnumerator, IEnumerable 
    { 
     public List<NumberList> NumberGroups = new List<NumberList>(); 

     #region EnumerationSetup 
     private int m_iPosition = -1; 
     public object Current 
     { 
      get 
      { 
       return NumberGroups; 
      } 
     } 

     public bool MoveNext() 
     { 
      m_iPosition++; 
      return true; 
     } 

     public void Reset() 
     { 
      m_iPosition = 0; 
     } 

     IEnumerator IEnumerable.GetEnumerator() 
     { 
      return NumberGroups.GetEnumerator(); 
     } 
     #endregion 
    } 
} 
+2

你爲什麼要實現自己的集合?它看起來像使用'List '直接應該適合你。 – svick

+0

這也許是一個功課問題?如果是這樣,請標記爲。您是否需要使用提供的方法簽名(AddEvenNumberToRange(NumberRanges,int)和AddOddNumberToRange(NumberRanges,int)? – Tim

+0

svick感謝列表暗示。 –

回答

0

List<T>真的是去(如svick指出的)的方式,但如果你必須做這種方式,它似乎對我下面將簡單:

private static void AddOddNumberToRange(NumberRanges oNumberRanges, int p) 
{ 
    oNumberRanges.NumberGroups[1].Number.Add(p); 
} 

private static void AddEvenNumberToRange(NumberRanges oNumberRanges, int p) 
{ 
    oNumberRanges.NumberGroups[0].Number.Add(p); 
} 

這將產生在第二次調用下面的輸出OutPutRanges:

 
2 
4 
6 
8 
3 
5 
7 
9 

使用List<T>

如果你想使用List<T>,你需要兩個List<int>(oGroupEven和oGroupOdd),以及一個用於List<List<int>> oNumberRanges。你可以,如果需要的話,實際上是擺脫oGroupEven和oGroupOdd的,並使用一個List<List<int>>把你上面的代碼,它凝結成的幾行代碼在Main方法:

static void Main(string[] args) 
    { 
     // Create the oNumberRanges and initialize it with the even and odd lists 
     List<List<int>> oNumberRanges = new List<List<int>>() { new List<int>(){ 2, 4, 6}, new List<int>() { 3, 5, 7 } }; 

     OutPutRanges(oNumberRanges); 
     Console.ReadKey(); 

     // Add 8 to the even list 
     oNumberRanges[0].Add(8); 
     // Add 9 to the odd list 
     oNumberRanges[1].Add(9); 

     OutPutRanges(oNumberRanges); 

     Console.ReadKey(); 
    } 

    private static void OutPutRanges(List<List<int>> oNumberRanges) 
    { 
     foreach (List<int> olist in oNumberRanges) 
     { 
      foreach (int iNum in olist) 
      { 
       Console.WriteLine(iNum.ToString()); 
      } 
     } 
    } 

這將產生以下的輸出:

 
2 
4 
6 
3 
5 
7 
2 
4 
6 
8 
3 
5 
7 
9 
+0

Tim,這正是我想要的。是否可以命名列表,然後按名稱訪問列表?而不是oNumberRanges [0] .Add(8); oNumberRanges [「even」]。Add(8)。如果是這樣,你能提供一個例子嗎? –

+0

@chris - 不開箱,至少不是我所知道的,你可以使用一個字典,列表的名字作爲關鍵字,但是隻有兩個列表可能是過度殺傷的。現在,如果你想要一個像助記符的東西用於代碼,你總是可以使用幾個變量來保存正確的索引(即,int odd = 1,int even = 0) - 然後將它們傳遞給List索引器(即oNumberRanges [even] .Add(8) )。 – Tim

+0

謝謝。已解決 –

相關問題