声明:本人大学生,技术能力不高,不喜勿喷,请以学习的态度看帖。

来源:互联网 发布:linux安装git服务器 编辑:程序博客网 时间:2024/05/21 21:45
                                                    我去前面探探路———提莫队长

不多说上代码 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;  //添加上这个命名空间

namespace ArrayList类使用
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList myArryList = new ArrayList(5);   //集合实例化
            Console.WriteLine("myArrayList初始化之后有{0}个元素",myArryList .Count );
            //Add方法用于向集合添加元素,且每次只添加一个元素
            myArryList.Add(12346);
            myArryList.Add('a');
            myArryList.Add("mystring");        
            myArryList.Add(25.63);
            myArryList.Add(10L);
            Console.WriteLine("使用Add方法添加5个元素后有{0}个元素",myArryList .Count );
            string[] mystringArray = { "张三", "李四", "小明", "小红" }; //实例化
            myArryList.AddRange(mystringArray);     
            Console.WriteLine("使用AddRange方法后有{0}个元素",myArryList .Count );
            //引用类型string object类是所有类型的基类
            //遍历集合元素

            foreach (object outelement in myArryList)
            {
                Console.WriteLine(outelement + "\t");
            }
            Console.WriteLine("====================================");
            Console.WriteLine("====================================");
            Console.WriteLine("现在删除元素");
           [color=#00FF00] //删除元素有4种方法
            //使用变量名.Remove(值)  值为集合中元素名 作用删除本元素           
            //          .RemoveAt(值) 值为数字        作用删除第几个元素         
            //          .RemovRange(值,值) 值为数字    作用从第几个开始删除几个
            //          .Clear                         作用清除所有元素[/color]
            Console.ReadKey();
        }
    }

}


后续我会不定时发博客

原创粉丝点击