C#入门7.10——ArrayList类中元素的删除

ArrayList类中元素的删除有以下四种方法:

1.ArrayList变量名.Remove(要删除的值);

2.ArrayList变量名.RemoveAt(索引值);

3.ArrayList变量名.RemoveRange(开始索引值,要删除的个数);

4.ArrayList变量名.Clear(); //清空。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引用命名空间
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            //元素删除有四种方法
            ArrayList myArrayList = new ArrayList(3);
            myArrayList.Add(1);
            myArrayList.Add("成功");
            myArrayList.Add(25.63);
            string[] mystringArray = { "张三","李四","王武","赵柳" };
            myArrayList.AddRange(mystringArray);
            Console.WriteLine("删除之前的内容:");
            foreach (object outElement in myArrayList) Console.Write(outElement);
            //Remove(值)
            Console.WriteLine("\n删除之后的内容为:");
            myArrayList.Remove("张三");
            foreach (object outElement in myArrayList) Console.Write(outElement+"\t");

            //RemoveAt(索引值)
            Console.WriteLine("\n删除 李四 之后的内容为:");
            myArrayList.RemoveAt(3);
            foreach (object outElement in myArrayList) Console.Write(outElement+"\t");
            //RemoveRange(起始索引,删除个数);
            Console.WriteLine("\n删除 王武 赵柳 之后的内容为:");
            myArrayList.RemoveRange(3, 2);
            foreach (object outElement in myArrayList) Console.Write(outElement+"\t");

            //Clear()
            Console.WriteLine("\n清除所有元素:");
            myArrayList.Clear();
            foreach (object outElement in myArrayList) Console.Write(outElement+"\t");

            Console.ReadKey();
        }

    }
}




你可能感兴趣的:(C#)