17. c# 泛型List

list泛型的用法和示例

using System;
using System.Collections.Generic;  //必须引用
using System.Linq;
using System.Text;

namespace _07List泛型集合
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            //集合--->数组
            //Count:获取集合中实际包含的元素的个数
            //Capcity:集合中可以包含的元素的个数

            //list.Add(1);
            //Console.WriteLine(list.Count);
            //Console.WriteLine(list.Capacity);

            //Add的是添加单个元素
            //AddRange是添加集合
            list.Add(100);
            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });

            //list.Remove(100);
            //list.RemoveAll(n => n > 3);

            //list.RemoveAt(3);

            //list.RemoveRange(1, 6);

            // list.Insert(1, 200);

            // list.InsertRange(0, new int[] { 5, 4, 3, 2, 1 });

            //集合跟数组之间的转换

            //集合----->数组
            int[] nums = list.ToArray();

            List<string> list2 = new List<string>();

            //list2.ToArray()

            int[] nums3 = { 1, 2, 3, 4, 5, 6 };

            List<int> list3 = nums3.ToList();

           
            for (int i = 0; i < list3.Count; i++)
            {
                Console.WriteLine(list3[i]);
            }

            Console.ReadKey();
        }
    }
}

示例二

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

namespace Ant18
{
    /// <summary>
    /// 泛型list
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //实例化类
            Student student1 = new Student() { Id = 1, Name = "小明" };
            Student student2 = new Student() { Id = 2, Name = "小阿萨" };
            Student student3 = new Student() { Id = 3, Name = "小阿三" };
            Student student4 = new Student() { Id = 4, Name = "小速度" };
            Student student5 = new Student() { Id = 5, Name = "暗示" };

            //泛型list的使用
            List<Student> stuList = new List<Student>();
            //往集合里添加数据
            stuList.Add(student1);
            stuList.Add(student2);
            stuList.Add(student3);
            stuList.Add(student4);

            //删除元素
            stuList.Remove(student1);
            //插入元素
            stuList.Insert(0, student5);

            //遍历
            foreach (Student item in stuList)
            {
                Console.WriteLine(item.Id.ToString() + " " +item.Name);
            }
            
            Console.WriteLine("--------------------------------");
            //-------------------------------
            //集合初始化器
            List<Student> stuList1 = new List<Student>()
            {
            new Student() { Id = 1, Name = "小明" },
            new Student() { Id = 2, Name = "小阿萨" },
            new Student() { Id = 3, Name = "小阿三" },
            new Student() { Id = 4, Name = "小速度" },
            new Student() { Id = 5, Name = "暗示" },
            };
            foreach (Student item in stuList1)
            {
                Console.WriteLine(item.Id.ToString() + " " + item.Name);
            }
            Console.ReadKey();
        }
    }
}

list泛型的练习:(1).去除两个集合中的重复值 (2).随机生成10个1-100之间的数放到List中,要求这10个数不能重复,并且都是偶数 (3).把分拣奇偶数的程序用泛型实现

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

namespace _08List泛型集合的三个练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //案例:两个(List)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。

            //集合初始化器
            //List<string> list1 = new List<string>() { "a", "b", "c", "d", "e" };
            //List<string> list2 = new List<string>() { "d", "e", "f", "g", "h" };

            ////让list1添加list2中的每个元素
            //for (int i = 0; i < list2.Count; i++)
            //{
            //    if (!list1.Contains(list2[i]))
            //    {
            //        list1.Add(list2[i]);
            //    }
            //}

            //foreach (string item in list1)
            //{
            //    Console.WriteLine(item);
            //}



            //案例:随机生成10个1-100之间的数放到List中,要求这10个数不能重复,并且都是偶数(添加10次,可能循环很多次。)
            //Random r = new Random();
            //List<int> list = new List<int>();
            //for (int i = 0; i <10; i++)
            //{
            //    int rNumber = r.Next(1, 101);
            //    if (!list.Contains(rNumber) && rNumber % 2 == 0)
            //    {
            //        list.Add(rNumber);
            //    }
            //    else
            //    {
            //        i--;
            //    }
            //}

            //foreach (int item in list)
            //{
            //    Console.WriteLine(item);
            //}


            //把分拣奇偶数的程序用泛型实现。List<int>
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            List<int> listJi = new List<int>();
            List<int> listOu = new List<int>();

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] % 2 == 0)
                {
                    listOu.Add(nums[i]);
                }
                else
                {
                    listJi.Add(nums[i]);
                }
            }

            //listJi.AddRange(listOu);

            //foreach (int item in listJi)
            //{
            //    Console.WriteLine(item);
            //}
            listOu.AddRange(listJi);
            foreach (int item in listOu)
            {
                Console.WriteLine(item);
            }


            Console.ReadKey();

        }
    }
}
原文地址:https://www.cnblogs.com/gice/p/13027534.html