C# 中 List 用法

C# 中 List 用法

List 类是 ArrayList 类的泛型等效类,某些情况下,用它比用数组和 ArrayList 都方便。

我们假设有一组数据,其中每一项数据都是一个结构。

 

public struct Item
{
    public int Id;
    public string DisplayText;
}
注意结构是不能给实例字段赋值的,即 public int Id = 1 是错误的。
using System.Collections.Generic;

List<Item> items = new List<Item>();

//添加
Item item1 = new Item();
item1.Id = 0;
item1.DisplayText = "水星";
items.Add(item1);

//添加
Item item2 = new Item();
item2.Id = 1;
item2.DisplayText = "地球";
items.Add(item2);

//修改
//这里使用的是结构,故不能直接用 items[1].DisplayText = "金星";,如果 Item 是类,则可以直接用。为什么呢?因为结构是按值传递的。
Item item = items[1];
item.DisplayText = "金星";
items[1] = item;

C# []、List、Array、ArrayList 区别及应用

[] 是针对特定类型固定长度的。

List 是针对特定类型任意长度的。

Array 是针对任意类型固定长度的。

ArrayList 是针对任意类型任意长度的。

Array 和 ArrayList 是通过存储 object 实现任意类型的,所以使用时要转换。

应用示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // System.Int32 是结构
        int[] arr = new int[] { 1, 2, 3 };
        Response.Write(arr[0]); // 1
        Change(arr);
        Response.Write(arr[0]); // 2

        // List 的命名空间是 System.Collections.Generic
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
        Response.Write(list[0]); // 1
        Change(list);
        Response.Write(list[0]); // 2

        // Array 的命名空间是 System
        Array array = Array.CreateInstance(System.Type.GetType("System.Int32"), 3); // Array 是抽象类,不能使用 new Array 创建。
        array.SetValue(1, 0);
        array.SetValue(2, 1);
        array.SetValue(3, 2);
        Response.Write(array.GetValue(0)); // 1
        Change(array);
        Response.Write(array.GetValue(0)); // 2

        // ArrayList 的命名空间是 System.Collections
        ArrayList arrayList = new ArrayList(3);
        arrayList.Add(1);
        arrayList.Add(2);
        arrayList.Add(3);
        Response.Write(arrayList[0]); // 1
        Change(arrayList);
        Response.Write(arrayList[0]); // 2
    }

    private void Change(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] *= 2;
        }
    }

    private void Change(List<int> list)
    {
        for (int i = 0; i < list.Count; i++) // 使用 Count
        {
            list[i] *= 2;
        }
    }


    private void Change(Array array)
    {
        for (int i = 0; i < array.Length; i++) // 使用 Length
        {
            array.SetValue((int)array.GetValue(i) * 2, i); // 需要类型转换
        }
    }

    private void Change(ArrayList arrayList)
    {
        for (int i = 0; i < arrayList.Count; i++) // 使用 Count
        {
            arrayList[i] = (int)arrayList[i] * 2; // 需要类型转换
        }
    }
}
 

认识 C# 的 ArrayList

C# 不支持动态数组,用 ArrayList 可以实现动态数组的功能。


您还会喜欢:C# []、Array、List、ArrayList 区别简介

ArrayList 的名称空间是 System.Collections。

ArrayList 元素类型

和数组不同,ArrayList 的各个元素的类型可以不同。

声明对象

//声明 ArrayList 有三种重载方法,较常用的有两种
ArrayList al = new ArrayList();
ArrayList al = new ArrayList(3);

上例中,参数值 3 表示容量,即可以容纳多少个元素。

Capacity 与 Count

ArrayList 具有 Capacity 和 Count 属性,分别表示容量和数量。

  • Capacity 表示 ArrayList 可包含的元素数;Count 表示实际包含的元素数。
  • Capacity 可读可写;Count 只读。

当 Capacity 不够时

我们的 Capacity 如果小了,无法容纳我们实际的元素个数,这时再增加元素时:

如果指定了 Capacity,Capacity 将按指定的 Capacity 的倍数增长(如果 Capacity 小于 2,将按 2 的倍数增长),比如指定了 Capacity 为 3,那么将按 3、6、9、12、15、18 ……线性的方式增长,直到可以容纳实际的元素个数。

如果没有指定 Capacity 值,Capacity 将按 2、4、8、16、32、64 ……指数的方式增长,直到可以容纳实际的元素个数。

Capacity 不会自动缩小

当 Capacity 被自动增大后,即使删除了某些元素,Capacity 也不会自动缩小,需要设置 Capacity 来缩小,注意 Capacity 不能小于当前 Count。

使用 C# 的 ArrayList

C# 不支持动态数组,用 ArrayList 可以实现动态数组的功能。


您还会喜欢:C# []、Array、List、ArrayList 区别简介

这里介绍一些 ArrayList 常用的代码,都是望名生义,不再举例。

获取元素值

object value = al[index]; //al 为 ArrayList 对象, 一般需要再对 value 进行类型转换,比如:int n = (int)value;

设置元素值

al[index] = value; //al 为 ArrayList 对象,index 必须小于 Count

追加元素

int ArrayList.Add(object value) 返回添加的元素的索引

插入元素

void ArrayList.Insert(int index, object value)

删除元素

删除元素后,后面的元素会前移,但 Capacity 不会变化。

void ArrayList.Remove(object obj) 从前(索引 0)往后查找,删除找到的第一个和 obj 相同的元素
void ArrayList.RemoveAt(int index) 删除索引 index 对应的元素
void ArrayList.RemoveRange(int index, int count) 从索引 index 开始,删除 count 个元素

查找元素

int ArrayList.IndexOf(object value) 从前(索引 0)往后查找,返回找到的第一个和 obj 相同的元素的索引
int ArrayList.IndexOf(object value, int startIndex)
int ArrayList.IndexOf(object value, int startIndex, int count)
int ArrayList.LastIndexOf(object value) 从后往前(索引 0)查找,返回找到的第一个和 obj 相同的元素的索引
int ArrayList.LastIndexOf(object value, int startIndex)
int ArrayList.LastIndexOf(object value, int startIndex, int count)
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值