ArrayList 跟 List 比较和用法
#region ArrayList ArrayList list = new ArrayList(); //新增数据 list.Add("abc"); list.Add(123); //修改数据 list[0] = 345; //移除数据 list.RemoveAt(2); //删除索引=2的元素 list.Remove(345); //从前(索引 0)往后查找,删除找到的第一个值是345的元素 //插入数据 list.Insert(0, "hello1 world"); //这样操作的时候会导致之前索引为0以及以后的值都往后移动一个位置 list.Add("a"); list.Add("b"); list.Add("c"); list.RemoveRange(2, 2);//从索引 index 开始,删除 count 个元素 list.Clear(); //删除所有的元素 //获取元素值 //int value1 = list[3]; //这个会报错:object类型不能转化 //成int类型,所以,所有塞进ArrayList里面的都会被转换成object类 //型(装箱的过程:就是将值类型的数据打包到引用类型) //查找元素 list.Add("a"); list.Add("b"); list.Add("c"); list.Add("d"); list.Add("c"); list.Add("e"); list.Add("f"); int index =list.IndexOf("c");//从前(索引 0)往后查找,返回找到的第一个 值=c 的索引 int index1 =list.IndexOf("c",3);//从前(索引 0)往后查找,返回找到的第一个 值=c 并且从第三个索引值开始找返回的索引值 int index2 =list.IndexOf("e",2,4);//从前(索引 0)往后查找,返回找到的第一个 值=e //并且从第2个索引值开始找只找连续的4个索引值(4个索引值里面包括第二个索引) int index3 = list.LastIndexOf("e");//从后往前找,找到e的索引值 int value = (int)list[3];//用下面的语句从 ArrayList 里面取出对应的值(拆箱的过程,就是从引用数据中提取值类型) #endregion #region List //由于塞进ArrayList所有的值都进行了装箱,转换成了object,这个过程是很浪费资源的 //所以泛型List才被引入 List<int> lsTest = new List<int>(); lsTest.Add(7); lsTest.Add(5); lsTest.Add(1); lsTest.Add(3); //lsTest.Add("hello world"); //这个是错误的,因为塞入的对象是string int value1 = lsTest[2]; //不需要进行拆箱的动作 //遍历List中元素: List<string> mList = new List<string>(); foreach (string s in mList) { Console.WriteLine(s); } //List也可以groupby和orderby,需要的时候再更新吧 #endregion