C#基礎[9] ArrayList集合(一)

來源:互聯網
上載者:User

標籤:數組   元素   一個   範圍   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集合(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.