在C#中,如果需要数组的长度和元素的个数随着程序的运行不断改变,就可以使用ArrayList类,该类是一个可以动态增减成员的数组。

一、ArrayList类的常用属性和方法

1. ArrayList类的常用属性

2. ArrayList类的常用方法

二、ArrayList类与Array类的区别

?ArrayList类实际上是Array类的优化版本。

?ArrayList只能定义一维数组,Arrays可以定义多维数组。

?ArrayList的下限始终为0,Array可以定义自己的下限。

?ArrayList的元素都是object类型的,因此需要进行装箱和拆箱操作,内存分配的代价很高,而Array的元素通常是特定类型的。

?ArrayList的元素个数可以自动扩展,Array的元素个数是固定的。

?ArrayList具有添加、删除、插入、移动和复制等方法。

三、示例
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; // 导入ArrayList的命名空间
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // C#动态数组ArrayList-www.baike369.com
            ArrayList alist = new ArrayList();
            alist.Add("One");        // 添加对象
            alist.Add(" Two");
            Console.Write("给数组添加的元素:");
            foreach (Object obj in alist)
            { Console.Write(obj); }
            Console.WriteLine();
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.Insert(1, " c#"); // 在指定的索引位置插入元素C#
            Console.Write("在索引值为1的位置插入:");
            foreach (Object obj in alist)
            { Console.Write(obj); }
            Console.WriteLine();
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.Add("。");
            Console.WriteLine("添加。");
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.Add("---"); // 集合容量不够时,会出现位数的变化
            Console.WriteLine("添加---");
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            // 用索引方式获取集合元素值
            Console.WriteLine("3号索引的:" + alist[3]);
            // 利用contains方法,查找集合中是否包含“?”
            Console.WriteLine("数组中是否包含?:" + alist.Contains("?"));
            Console.Write("经过前面操作后的数据元素:");
            foreach (Object obj in alist)
            { Console.Write(obj); }
            Console.WriteLine();
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.Remove("。"); // 移除集合中的“。”元素
            alist.Remove("?");  // 移除集合中的“?”元素
            Console.WriteLine("没有包含?,减少1个元素,容量不变");
            foreach (Object obj in alist)
            { Console.Write(obj); }
            Console.WriteLine();
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.RemoveAt(3);  // 移除3号索引位的元素
            Console.Write("移除3号索引位的元素:");
            foreach (Object obj in alist)
            { Console.Write(obj); }
            Console.WriteLine();
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.TrimToSize();  // 缩减容量
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.Clear();       // 清除ArrayList中的所有元素
            Console.WriteLine("清除ArrayList中的所有元素后:");
            Console.WriteLine("实际包含的数组元素个数:" + alist.Count);
            Console.WriteLine("目前能够包含的数组元素的最多个数:"+alist.Capacity);
            alist.TrimToSize();  // 再次缩减容量
            Console.WriteLine("再次缩减容量后,能够包含的数组元素的最多个数:"
                +alist.Capacity);
            Console.ReadLine();
        }
    }
}

运行结果:

C#动态数组ArrayList的更多相关文章

  1. 数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解

    数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解 对数组有不了解的可以先看看我的另一篇文章,那篇文章对数组有很多详细的解析,而本篇文章则着重讲动态数组,另一篇文章链接 ...

  2. [数据结构1.2-线性表] 动态数组ArrayList(.NET源码学习)

    [数据结构1.2-线性表] 动态数组ArrayList(.NET源码学习) 在C#中,存在常见的九种集合类型:动态数组ArrayList.列表List.排序列表SortedList.哈希表HashTa ...

  3. C#动态数组ArrayList和List<T>的比较

    C#中一维动态数组(即列表)分ArrayList和List<T>两种,其容量可随着我们的需要自动进行扩充 一.ArrayList类(少用) ArrayList位于System.Collec ...

  4. 关于C#中的动态数组ArrayList

    在C#中,如果需要数组的长度和元素的个数随着程序的运行不断改变,就可以使用ArrayList类,该类是一个可以动态增减成员的数组. 一.ArrayList类与Array类的区别 ArrayList类实 ...

  5. C# 集合类(一)动态数组ArrayList

    C# 集合类自己经常用到: 数组(Array).动态数组(ArrayList).列表(List).哈希表(Hashtable).字典(Dictionary),对于经常使用的这些数据结构,做一个总结,便 ...

  6. 动态数组 - ArrayList

    前言 如果数组的大小要随时间变化,那么数组操作起来就比较麻烦. 在C++中,这种情况要用到动态向量Vector. 而Java中,提供了一种叫做ArrayList的泛型数组结构类型,提供相似的作用. 其 ...

  7. 动态数组ArrayList的使用

    1.定义类 package com.realhope.rmeal.bean; /** * * @author Wucy * 菜谱类 */ public class Menu{ private Inte ...

  8. C#深入研究ArrayList动态数组自动扩容原理

    1 void Test1() { ArrayList arrayList = new ArrayList(); ; ; i < length; i++) { arrayList.Add(&quo ...

  9. 常用数据结构-线性表及Java 动态数组 深究

    [Java心得总结六]Java容器中——Collection在前面自己总结的一篇博文中对Collection的框架结构做了整理,这里深究一下Java中list的实现方式 1.动态数组 In compu ...

随机推荐

  1. ue4 svn备份目录

    http://blog.csdn.net/sh15285118586/article/details/55737480 UE4工程文件备份目录有:Config.Content.Plugins.Sour ...

  2. 一些unity资源

    雨凇解包 http://www.xuanyusong.com/archives/3618 http://www.cnblogs.com/lixiang-share/p/5840444.html u3d ...

  3. MySQL学习基础之一 — 数据库查询

    廖大神的练手神器:在线SQL: https://www.liaoxuefeng.com/wiki/1177760294764384/1179611432985088 运行MySQL等实际的数据库软件, ...

  4. 内置对象(Math对象、Date对象、Array对象、String对象)常用属性和方法

    Math对象 Math 是一个内置对象, 它具有数学常数和函数的属性和方法.不是一个函数对象. 与其它全局对象不同的是, Math 不是一个构造函数.  Math 的所有属性和方法都是静态的. 跟数学 ...

  5. HTML5新标签介绍

    一.Datalist 标签 <input list="browsers"> <datalist id="browsers">   < ...

  6. Visio画图(一):UML用例图

    Visio画图(一):UML用例图 1.找到UML用例图 A.有网状态 第一步 在搜索框内输入用例图进行搜索. 第二步,移动鼠标直到找到用例图 B.无网状态 第一步 点击特别推荐旁的类别选项 第二步 ...

  7. day5字典作业详解

    1.day5题目 1.有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2&q ...

  8. nutzboot 使用nacos替换zookeeper

    先放一段从网上拷贝一段分布式CAP理论的概念 分布式领域中存在CAP理论,且该理论已被证明:任何分布式系统只可同时满足两点,无法三者兼顾. ①C:Consistency,一致性,数据一致更新,所有数据 ...

  9. VUE中实现iview的图标效果时遇到的一个问题

    [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available ...

  10. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) A

    Arpa is researching the Mexican wave. There are n spectators in the stadium, labeled from 1 to n. Th ...