//将一个数组中的奇数放到集合中,再将偶数放到另一个集合中。
int[] a = { 6,7,4,3,2,1,8,9,11,16};
List<int> b = new List<int>();//存储偶数集合
List<int> c = new List<int>();//存储奇数集合
for (int i = 0; i <a.Length ; i++)
{
if (a[i] % 2 == 0)
{
b.Add(a[i]);
}
if (a[i] % 2 != 0)
{
c.Add(a[i]);
}
}
c.AddRange(b);
for (int i = 0; i <c.Count; i++)
{
Console.Write(c[i]+" ");
}
Console.ReadKey();
List泛型集合的应用(数据的添加)
最新推荐文章于 2024-07-02 10:29:53 发布