C# 集合類(一):ArrayList

來源:互聯網
上載者:User
System.Collections 命名空間包含介面和類,這些介面和類定義各種對象(如列表、隊列、位元組、雜湊表和字典)的集合。
System.Collections.Generic 命名空間包含定義泛型集合的介面和類,泛型集合允許使用者建立強型別集合,它能提供比非泛型強型別集合更好的型別安全和效能。
System.Collections.Specialized 命名空間包含專用的和強型別的集合,例如,連結的列表詞典、位向量以及只包含字串的集合。

ArrayList 類:使用大小可按需動態增加的數組。using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            al.Add(100);//單個添加
            foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
            {
                al.Add(number);//集體添加方法一
            }
            int[] number2 = new int[2] { 11, 12 };
            al.AddRange(number2);//集體添加方法二
            al.Remove(3);//移除值為3的
            al.RemoveAt(3);//移除第3個
            ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取舊ArrayList一部份


            Console.WriteLine("遍曆方法一:");
            foreach (int i in al)//不要強制轉換
            {
                Console.WriteLine(i);//遍曆方法一
            }

            Console.WriteLine("遍曆方法二:");
            for (int i = 0; i < al2.Count; i++)//數組是length
            {
                int number = (int)al2[i];//一定要強制轉換
                Console.WriteLine(number);//遍曆方法二

            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.