C#中的ArrayList类详解

目录

一、ArrayList类定义

二、ArrayList类与数组的区别

三、ArrayList类对象的声明

1、默认的构造器

2、用一个ICollection对象来构造

3、用指定的大小初始化内部的数组

四、ArrayList常用属性

五、ArrayList常用方法

1、ArrayList元素的添加 

(1)Add()方法

(2)Insert()方法

(3)综合例

2、ArrayList元素的删除

(1)Clear()方法

(2)Remove()方法

(3)RemoveAt()方法

(4)RemoveRange()方法

(5)综合例

3、ArrayList元素的查找

(1)Contains()方法

(2)IndexOf()方法

(3)LastIndexOf()方法

4、ArrayList的遍历

一、ArrayList类定义

          ArrayList类相当于一种高级的动态数组,它是Array类的升级版本。

        ArrayList类位于System.Collections命名空间下,它可以动态地添加和删除元素。可以将ArrayList类看作扩充了功能的数组,但它并不等同于数组。使用ArrayList类要添加引用:

        using System.Collections;

二、ArrayList类与数组的区别

        与数组相比,ArrayList类为开发人员提供了以下功能。

        ☑ 数组的容量是固定的,而ArrayList的容量可以根据需要自动扩充。

        ☑ ArrayList提供添加、删除和插入某一范围元素的方法,但在数组中,只能一次获取或设置一个元素的值。

        ☑ ArrayList提供将只读和固定大小包装返回集合的方法,而数组不提供。

        ☑ ArrayList只能是一维形式,而数组可以是多维的。

        Array的长度是它可包含的元素总数。Array的秩是Array中的维数。Array中维度的下限是Array中该维度的起始索引,多维Array的各个维度可以有不同的界限。

三、ArrayList类对象的声明

        ArrayList提供了3个构造器,通过这3个构造器可以有3种声明方式:

1、默认的构造器

        以默认(16位)的大小来初始化内部的数组。

        构造器格式:public ArrayList();

        通过以上构造器声明ArrayList的语法格式:

        ArrayList List=new(); //ListArrayList对象名。

ArrayList List=new();	 //List:ArrayList对象名
for(i=0;i<10;i++)        //给ArrayList类对象添加10个int型元素
{
    List.Add(i);
}

2、用一个ICollection对象来构造

        并将该集合的元素添加到ArrayList中。

        构造器格式:public ArrayList(ICollection);

        通过以上构造器声明ArrayList的语法格式:

        ArrayList List=new( arrayName);

        ☑ List:ArrayList对象名。

        ☑ arryName:要添加集合的数组名。

int[]arr=new int[]{1,2,3,4,5,6,7,8,9};
ArrayList List=new(arr);

3、用指定的大小初始化内部的数组

        构造器格式:public ArrayList(int);

        通过以上构造器声明ArrayList的语法格式:

        ArrayList List=new(n);

        ☑ List:ArrayList对象名。

        ☑ n:ArrayList对象的空间大小。

ArrayList List=new(10);
for(int i=0;i<List.Count;i++)
{
    List.Add(i);    //给ArrayList添加10个int型元素
}

四、ArrayList常用属性

属性

说明

Capacity

获取或设置ArrayList可包含的元素数

Count

获取ArrayList实际包含的元素数

IsFixedSize

获取一个值,该值指示ArrayList是否具有固定的大小

IsReadOnly

获取一个值,该值指示ArrayList是否为只读

IsSyncronized

获取一个值,该值指示是否对ArrayList同步访问

Item

获取或设置指定索引处的元素

SyncRoot

获取可用于同步ArrayList访问的对象

五、ArrayList常用方法

1、ArrayList元素的添加 

(1)Add()方法

        Add()方法用来将对象添加到ArrayList集合的结尾处,其语法格式:

        public virtual int Add(Object value);

        ☑ value:要添加到ArrayList的末尾处的Object,该值可以为空引用。

        ☑ 返回值:ArrayList索引,已在此处添加了value。

        ArrayList允许null值作为有效值,并且允许重复的元素。

int arr[] =new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	//使用声明的数组实例化ArrayList对象
List.Add(7);			    //为ArrayList对象添加元素

(2)Insert()方法

        Insert()方法用来将元素插入ArrayList集合的指定索引处,其语法格式:

        public virtual void Insert(int index, Object value)

        ☑ index:从零开始的索引,应在该位置插入value。

        ☑ value:要插入的Object,该值可以为空引用。

        如果ArrayList实际存储元素数已经等于ArrayList可存储的元素数,则会通过自动重新分配内部数组增加ArrayList的容量,并在添加新元素之前将现有元素复制到新数组中。 

int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new(arr);	//使用声明的数组实例化ArrayList对象
List.Insert(3,7);			//在ArrayList对象索引值=3处添加元素7

(3)综合例

//Add()方法和Insert()方法
//分别使用ArrayList对象的Add()方法和Insert()方法向ArrayList集合的结尾处和指定索引处添加元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Test7_12
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)                           //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }
            int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
            ArrayList List = new(arr);                  //使用声明的一维数组实例化一个ArrayList对象
            Console.WriteLine("原ArrayList集合:");
            foreach (int i in List)					    //遍历ArrayList源集合并输出
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            for (int i = 1; i < 5; i++)
            {
                List.Add(i + arr.Length);			    //使用Add()方法为ArrayList集合添加元素
            }
            Console.WriteLine("使用Add方法添加:");
            foreach (int i in List)					    //遍历添加元素后的ArrayList集合并输出
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            List.Insert(6,0);						    //使用Insert()方法在ArrayList集合的index=6处添加元素0
            Console.WriteLine("使用Insert方法添加:");
            foreach (int i in List)					    //遍历最后的ArrayList集合并输出
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}
/*运行结果:
原ArrayList集合:
1 2 3 4 5 6
使用Add方法添加:
1 2 3 4 5 6 7 8 9 10
使用Insert方法添加:
1 2 3 4 5 6 0 7 8 9 10  */

2、ArrayList元素的删除

(1)Clear()方法

        Clear()方法用来从ArrayList中移除所有元素,其语法格式:

        public virtual void Clear()

//使用 Clear()方法清除ArrayList中的所有元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	//使用声明的数组实例化ArrayList对象
List.Clear();			    //在ArrayList对象指定位置添加元素

(2)Remove()方法

        Remove()方法用来从ArrayList中移除特定对象的第一个匹配项,其语法格式:

        public virtual void Remove(Object obj)

        obj:要从ArrayList移除的Object,该值可以为空引用。

        在删除ArrayList中的元素时,如果不包含指定对象,则ArrayList将保持不变。

//使用RemoveAt()方法从声明的ArrayList对象中移除与3匹配的元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new(arr);	//使用声明的数组实例化ArrayList对象
List.Remove(3);			    //删除ArrayList对象指定位置元素

(3)RemoveAt()方法

        RemoveAt()方法用来移除ArrayList的指定索引处的元素,其语法格式:

        public virtual void RemoveAt(int index)

        index:要移除的元素的从零开始的索引。        

//使用RemoveAt()方法从声明的ArrayList对象中移除索引为3的元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = newt(arr);	//使用声明的数组实例化ArrayList对象
List.RemoveAt(3);		    //删除ArrayList对象指定位置元素

(4)RemoveRange()方法

        RemoveRange()方法用来从ArrayList中移除一定范围的元素,其语法格式:

        public virtual void RemoveRange(int index,int count)

        ☑ index:要移除的元素的范围从零开始的起始索引。

        ☑ count:要移除的元素数。

        在RemoveRange()方法中参数count的长度不能超出数组的总长度减去参数index的值。

//在ArrayList对象中使用RemoveRange()方法从索引3处删除两个元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	   //使用声明的数组实例化ArrayList对象
List.RemoveRange(3,2);	       //删除ArrayList对象指定位置3处2个元素

(5)综合例

//RemoveRange()
//使用RemoveRange()方法删除ArrayList集合中的一批元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Test7_13
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)           //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }
            int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            ArrayList List = new(arr);  //使用声明的一维数组实例化一个ArrayList对象
            Console.WriteLine("原ArrayList集合:");
            foreach (int i in List)     //遍历ArrayList集合中的元素并输出
            {
                /*Console.Write(i+ " ");*/  //可以注释掉.ToString()不影响结果
                Console.Write(i.ToString() + " ");
            }

            Console.WriteLine();
            List.RemoveRange(0, 5);     //从索引0处开始删除5个元素
            Console.WriteLine("删除元素后的ArrayList集合:");
            foreach (int i in List)     //遍历删除元素后的ArrayList集合并输出其元素
            {
                Console.Write(i.ToString() + " ");
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}
/*运行结果:
原ArrayList集合:
1 2 3 4 5 6 7 8 9
删除元素后的ArrayList集合:
6 7 8 9     */

3、ArrayList元素的查找

(1)Contains()方法

        Contains()方法用来确定某元素是否在ArrayList集合中,其语法格式:

        public virtual bool Contains(Object item)

        ☑ item:要在ArrayList中查找的Object,该值可以为空引用。

        ☑ 返回值:如果在ArrayList中找到item,则为true;否则为false。

//使用 Contains()方法判断数字2是否在ArrayList集合中
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	   	    //使用声明的数组实例化ArrayList对象
Console.Write(List.Contains(2));	//ArrayList集合中是否包含指定的元素

(2)IndexOf()方法

        用于查找字符或者字符串首次出现的位置。

        IndexOf() 方法由以下语法表示:

public int IndexOf(String value);                  //语法1
public int IndexOf(char value);                    //语法2
public int IndexOf(String value, int startIndex);  //语法3
public int IndexOf(char value, int startIndex);    //语法4

☑ value:指定要查找的字符或者字符串。

☑startIndex:指定要查找开始的位置。

返回字符或者字符串首次出现的位置,索引值从0开始计算。

//IndexOf()
//查找字符或者字符首次出现的位置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace yxjc123
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)                                           //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }
            string str = "yxjc123.com  repeat yxjc123.com";            //原字符串

            Console.WriteLine("查找j:" + str.IndexOf('j'));           //查找字符j
            Console.WriteLine("查找123:" + str.IndexOf("123"));       //查找字符串123

            Console.WriteLine("查找j:" + str.IndexOf('j', 10));       //查找字符j,从开始位置10开始
            Console.WriteLine("查找123:" + str.IndexOf("123", 10));   //查找字符串123,从开始位置10开始

            Console.ReadKey();
        }
    }
}
/*运行结果:
查找j:2
查找123:4
查找j:22
查找123:24    */

(3)LastIndexOf()方法

        用于查找字符或者字符串最后出现的位置。

        LastIndexOf() 方法由以下语法表示:

public int LastIndexOf(String value);                   //语法1
public int LastIndexOf(char value);                     //语法2
public int LastIndexOf(String value, int startIndex);   //语法3
public int LastIndexOf(char value, int startIndex);     //语法4

        ☑ value:指定要查找的字符或者字符串。

        ☑ startIndex:指定要查找开始的位置,从末尾开始计算

        返回字符或者字符串最后出现的位置。

//LastIndexOf()
//用于查找字符或者字符串最后出现的位置

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

namespace yxjc123
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)                                               //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }
            string str = "yxjc123.com  repeat yxjc123.com";                 //原字符串

            Console.WriteLine("查找j:" + str.LastIndexOf('j'));           //查找字符j
            Console.WriteLine("查找123:" + str.LastIndexOf("123"));       //查找字符串123

            Console.WriteLine("查找j:" + str.LastIndexOf('j', 10));       //查找字符j,倒数从开始位置10开始
            Console.WriteLine("查找123:" + str.LastIndexOf("123", 10));   //查找字符串123,倒数从开始位置10开始
            Console.ReadKey();
        }
    }
}
/*运行结果:
查找j:22
查找123:24
查找j:2
查找123:4   */

4、ArrayList的遍历

        ArrayList集合的遍历与数组类似,都可以使用foreach语句,下面通过一个实例说明如何遍历ArrayList集合中的元素。

//ArrayList的遍历
//使用foreach语句遍历ArrayList集合中的各个元素并输出

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

namespace Test7_14
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)               //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }
            ArrayList list = new()          //实例化一个ArrayList对象并赋值
            {
                "TM",
                "C#从入门到精通"
            };                              
            foreach (string str in list)    //遍历ArrayList集合中的元素并输出
            {
                Console.WriteLine(str);
            }
            Console.ReadLine();
        }
    }
}
/*运行结果:
TM
C#从入门到精通    */

猜你喜欢

转载自blog.csdn.net/wenchm/article/details/131480220