標籤:數組 元素 一個 範圍 void contains 集合 使用 bsp
集合
可以看做數組
-> 建立一個長度為10的數組
數組長度為10,要插入新的資料,就得重新建立數組 排序
-> 長度固定,不太靈活
使用集合即可方便解決這些問題
可以將集合看作為“長度可變的,具有很多方法的數組”
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace _07ArrayList的各種方法{ class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); //添加單個元素 list.Add(true); list.Add(1); list.Add("張三"); //添加集合元素 list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); //list.AddRange(list); //list.Clear();清空所有元素 //list.Remove(true);刪除單個元素 寫誰就刪誰 //list.RemoveAt(0);根據下標去刪除元素 //list.RemoveRange(0, 3);根據下標去移除一定範圍的元素 //list.Sort();//升序排列 //list.Reverse();反轉 //list.Insert(1, "插入的");在指定的位置插入一個元素 //list.InsertRange(0, new string[] { "張三", "李四" });在指定的位置插入一個集合 //bool b = list.Contains(1);判斷是否包含某個指定的元素 list.Add("顏世偉"); if (!list.Contains("顏世偉")) { list.Add("顏世偉"); } else { Console.WriteLine("已經有這個屌絲啦"); } for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); } Console.ReadKey(); } }}
C#基礎[9] ArrayList集合(一)