理想情況下,我想添加到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
}
}
你爲什麼要實現自己的集合?它看起來像使用'List'直接應該適合你。 –
svick
這也許是一個功課問題?如果是這樣,請標記爲。您是否需要使用提供的方法簽名(AddEvenNumberToRange(NumberRanges,int)和AddOddNumberToRange(NumberRanges,int)? – Tim
svick感謝列表暗示。 –