集合与数组比较:
(1)长度可以改变
(2)添加数据时,不必考虑具体类型
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace GenericTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> list = new List<int>();
- list.Add(1);
- list.Add(2);
- list.Add(3);
- list.AddRange(new int[]{4,5,6,7,8});
- list.AddRange(list);//将自身作为参数添加到列表
- Console.WriteLine("======输出=======");
- for(int i=0;i
- Console.WriteLine(list[i]);
- Console.WriteLine("======反转=======");
- list.Reverse();
- for (int i = 0; i < list.Count; i++)
- Console.WriteLine(list[i]);
- Console.WriteLine("======排序大到小=======");
- list.Sort();
- for (int i = 0; i < list.Count; i++)
- Console.WriteLine(list[i]);
- Console.WriteLine("======插入=======");
- list.Insert(3,100);
- for (int i = 0; i < list.Count; i++)
- Console.WriteLine(list[i]);
- Console.WriteLine("======删除=======");
- for (int i = 0; i < list.Count; i++)
- Console.WriteLine(list[i]);
- Console.WriteLine("======数组与列表互转=======");
- int[] nums = list.ToArray();
- }
- }
- }