理想情况下,我想添加到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
}
}
答案 0 :(得分: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