震驚!大學都快畢業了,某男子居然還不知道ArrayList集合是什麼

列表集合 ArrayList

ArrayList集合的使用

​ ArrayList非常類似於數組,因爲其容量可按照需要動態調整,且也是通過下標訪問元素,

也稱之爲動態數組。

ArrayList集合常用的屬性中存在兩個重要屬性:Capacity和Count。其中Capacity用於獲取集合的容量,Count用於獲取集合中存儲元素的個數。

​ 與使用數組不同,創建 ArrayList對象時,不需要指定容量,其容量將會動態調整。

下面通過一個案例演示一下ArrayList集合的容量變化規則。

新建一個Customer類,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LieBiaoJiHe
{
    public class Customer
    {
        //定義一個無參的構造方法
        public Customer() { }
        //定義一個有參數的構造方法
        public Customer(string name, int age, string address)
        {
            Name = name;
            Age = age;
            Address = address;
        }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
    }
}

主方法中代碼如下:當我們設置 ArrayList的容量爲2時,如果容量超出,結果會怎樣?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LieBiaoJiHe
{
    class Program
    {
        static void Main(string[] args)
        {

            //創建兩個Customer對象並實例化
            Customer customer_1 = new Customer("張三", 18, "北京");
            Customer customer_2 = new Customer("李四", 19, "上海");
            Customer customer_3 = new Customer("王五", 20, "深圳");
            //創建一個集合對象
            ArrayList list = new ArrayList(2);//設置容量爲2

            Console.WriteLine("**********");
            Console.WriteLine("集合容量:"+list.Capacity);
            Console.WriteLine("數據個數:"+list.Count);
            list.Add(customer_1);
            Console.WriteLine("**********");
            Console.WriteLine("集合容量:" + list.Capacity);
            Console.WriteLine("數據個數:" + list.Count);
            list.Add(customer_2);
            Console.WriteLine("**********");
            Console.WriteLine("集合容量:" + list.Capacity);
            Console.WriteLine("數據個數:" + list.Count);
            list.Add(customer_3);
            Console.WriteLine("**********");
            Console.WriteLine("集合容量:" + list.Capacity);
            Console.WriteLine("數據個數:" + list.Count);
        }
    }
}

運行結果如圖所示:

在這裏插入圖片描述
我們手動設置了ArrayList集合的爲2後,當集合中的對象有3個時,超出集合容量,此時ArrayList集合的容量會自動擴大一倍。以此類推,當集合中的元素有5個時,集合的容量就變成8了。由此我們可以得出一個規律:ArrayList中存儲的元素超出容量時,其容量將自動增長一倍。

ArrayList集合常用的方法

第一類:用於添加元素的方法:Add、Insert

第二類:用於刪除元素的方法:Remove、RomoveAt、Clear

第三類:排序和反轉的方法:Sort、Reverse

方法 作用
Add 將元素添加到ArrayList結尾處
Insert 將元素添加到ArrayList的指定索引處
Remove 通過對象刪除,若對象存在則刪除成功;否則,刪除失敗
RomoveAt 刪除指定索引處的元素
Clear 將集合中的所有元素清除
Sort 將集合中的元素進行排序
Reverse 將集合中的元素順序進行反轉

添加元素方法的使用:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LieBiaoJiHe
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建兩個Customer對象並實例化
            Customer customer_1 = new Customer("張三", 18, "北京");
            Customer customer_2 = new Customer("李四", 19, "上海");
            Customer customer_3 = new Customer("王五", 20, "深圳");
            //創建一個集合對象
            ArrayList list = new ArrayList();
            //添加元素
            list.Add(customer_1);  //Add,添加到ArrayList結尾處
            list.Add(customer_2);
            //此時ArrayList裏面存儲元素的順序爲:0:customer_1,1:customer_2

            list.Insert(1, customer_3);  //Insert,添加到ArrayList的指定索引處
          //此時ArrayList裏面存儲元素的順序爲:0:customer_1,1:customer_3,2:customer_2
            
            //ArrayList和數組一樣,通過下標對元素進行訪問,下標從0開始,且需要進行類型轉換。
            //獲取數據
            /*方法1,不建議使用,代碼太繁瑣
            Customer temp = list[0] as Customer;  //將list轉換爲Customer類型
            Console.WriteLine("客戶姓名:" + temp.Name);
            temp = list[1] as Customer;
            Console.WriteLine("客戶姓名:" + temp.Name);
            temp = list[2] as Customer;
            Console.WriteLine("客戶姓名:" + temp.Name);
            */
            //方法2,使用for循環獲取數據
            Console.WriteLine("使用for循環獲取數據");
            for (int i = 0; i < list.Count; i++)
            {
                Customer temp = list[i] as Customer; //將list轉換爲Customer類型
                Console.WriteLine("客戶姓名:"+temp.Name);
            }
            Console.WriteLine("*****************");
            //方法3,使用foreach循環獲取數據
            Console.WriteLine("使用foreach循環獲取數據");
            foreach (object item in list) 
            {
                Customer temp = item as Customer;//類型轉換,將item轉換爲Customer類型
                Console.WriteLine("客戶姓名:" + temp.Name);
            }
        }
    }
}

刪除元素方法的使用:

在ArrayList中,刪除靠前的元素,之後的元素將自動向前移動。

namespace LieBiaoJiHe
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建兩個Customer對象並實例化
            Customer customer_1 = new Customer("張三", 18, "北京");
            Customer customer_2 = new Customer("李四", 19, "上海");
            Customer customer_3 = new Customer("王五", 20, "深圳");
            //創建一個集合對象
            ArrayList list = new ArrayList();
            //添加元素
            list.Add(customer_1);  //Add,添加到ArrayList結尾處
            list.Add(customer_2);
            list.Insert(1, customer_3);
            //刪除元素
            list.Remove(customer_1);
            Customer customer_4 = new Customer("王五", 20, "深圳");
            list.Remove(customer_4);//刪除失敗,customer_4沒有添加到集合中來
            //此時集合中元素爲 0:customer_3, 1:customer_2
            list.RemoveAt(1);//刪除索引位置在1的元素
            //此時集合中元素爲 0:customer_3
            list.Clear();  //清除集合中的所有元素

            //使用for循環獲取數據
            Console.WriteLine("使用for循環獲取數據");
            for (int i = 0; i < list.Count; i++)
            {
                Customer temp = list[i] as Customer; //將list轉換爲Customer類型
                Console.WriteLine("客戶姓名:"+temp.Name);
            }
            Console.WriteLine("*****************");
            //使用foreach循環獲取數據
            Console.WriteLine("使用foreach循環獲取數據");
            foreach (object item in list) 
            {
                Customer temp = item as Customer;//類型轉換,將item轉換爲Customer類型
                Console.WriteLine("客戶姓名:" + temp.Name);
            }
        }
    }
}

排序方法的使用:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LieBiaoJiHe
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建兩個Customer對象並實例化
            Customer customer_1 = new Customer("張三", 18, "北京");
            Customer customer_2 = new Customer("李四", 19, "上海");
            Customer customer_3 = new Customer("王五", 20, "深圳");
            Customer customer_4 = new Customer("趙六", 16, "廣州");
            //創建一個集合對象
            ArrayList list = new ArrayList();
            //添加元素
            list.Add(customer_1.Age); 
            list.Add(customer_2.Age);
            list.Add(customer_3.Age);
            list.Add(customer_4.Age);
            list.Sort();//默認按照升序進行排列
            list.Reverse();//反轉集合中的元素,此時排序方式由原來的升序變成了降序
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

使用Sort方法排序後的結果如圖所示:Sort方法默認爲升序進行排列

在這裏插入圖片描述
使用Reverse方法進行反轉後的結果如圖所示:由原來的升序變成了降序
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章