How to Add to a List Collection Once Added to Another List Collection?
Ideally, I want to add to the NumberList, without deleting it Makinging another NumberList, and adding that to 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 olis开发者_开发技巧t)
{
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<T>
really is the way to go (as svick pointed out), but if you must do it this way, it would seem to me the following would be simplest:
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);
}
This will produce the following output on the second call to OutPutRanges:
2 4 6 8 3 5 7 9
Using List<T>
If you wanted to use List<T>
, you'd need two List<int>
(oGroupEven and oGroupOdd), and one List<List<int>>
for oNumberRanges. You could, if desired, actually get rid of oGroupEven and oGroupOdd and use one List<List<int>>
to take your above code and condense it into a few lines of code in the Main method:
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());
}
}
}
Which would produce the following output:
2 4 6 3 5 7 2 4 6 8 3 5 7 9
精彩评论