ArrayList:

来源:互联网 发布:sig516突击步枪知乎 编辑:程序博客网 时间:2024/06/05 19:34

(1).默认构造器:ArrayList List = new ArrayList();

eg: ArrayList List = new ArrayList();
            for (int i = 0; i < 10; i++)
            {
                List.Add(i);
            }
            foreach (int m in List)
            {
                Console.Write(m+" ");
            }
            Console.ReadLine();

(2)用一个ICollection对象来构造,并将集合的元素添加到ArrayList中

eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            ArrayList List = new ArrayList(arr);
            foreach (int m in List)
            {
                Console.Write(m + " ");
            }
            Console.ReadLine();

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

eg: ArrayList MY = new ArrayList(10);
            for(int i = 0; i < MY.Count; i++)
            {               
                MY.Add(i);
            }

添加ArrayList元素

(1)Add方法:public  virtual  int   Add(object  value)

 int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
            ArrayList List = new ArrayList(arr);
            List.Add(7);    //在ArrayList的尾部添加
            foreach (int m in List)
            {
                Console.Write(m + " ");
            }
            Console.ReadLine();

(2)Insert方法:public  virtual  void Insert(int  index,object  value)

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

eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
            ArrayList List = new ArrayList(arr);
            List.Insert(3,7);  //在ArrayList集合的指定索引处添加元素
            foreach (int str in List)
            {
                Console.WriteLine(str);
            }
            Console.ReadLine();


删除ArrayList元素

(1)Clear方法:public virtual void Clear()

eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
            ArrayList List = new ArrayList(arr);
            List.Clear();     //清除ArrayList集合的元素
            foreach (int m in List)
            {
                Console.Write(m + " ");
            }
            Console.ReadLine();

(2)Remove方法:public  virtual  void Remove(object  obj)

 eg:int[] arr = new int[] {1,2,3,4,5,6 };
            ArrayList List = new ArrayList(arr);
            List.Remove(3);     //一处ArrayList集合中与3匹配的元素
            foreach (int m in List)
            {
                Console.Write(m + " ");
            }
            Console.ReadLine();

(3)RemoveAt方法:public   virtual   void RemoveAt(int   index)

eg:    int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
            ArrayList List = new ArrayList(arr);
            List.RemoveAt(3);      //移除ArrayList集合中索引为3的元素
            foreach (int m in List)
            {
                Console.Write(m + " ");
            }
            Console.ReadLine();

(4)RemoveRange方法:public   virtual   void RemoveRange(int   index,int   count)

 eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
            ArrayList List = new ArrayList(arr);
            List.RemoveRange(3,2);     //从ArrayList集合的索引3处移除两个元素
            foreach (int m in List)
            {
                Console.Write(m + " ");
            }
            Console.ReadLine();

利用以上介绍的方法 编程实现:

定义一个teacher 类,包含姓名和职称和一个输出自己信息的方法,建立arraylist类动态数组将3个teacher类对象的增加到数组中,在索引值为1的位置 插入一个 teacher类对象,删除索引值为1 的元素,最后输出数组中 所有元素的信息

可参看 教材 154-157页

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace _1

{

    class Program

    {

        static void Main(string[] args)

        {

            teacher p1 = new teacher("张三1","软件工程师1" );

            teacher p2 = new teacher("张三2","软件工程师2");

            teacher p3 = new teacher("张三3","软件工程师3");

            ArrayList aList = new ArrayList();

            aList.Add(p1);

            aList.Add(p2);

            aList.Add(p3);

            aList.RemoveAt(1);        //删除下标1的元素,后面元素依次向前移动

            for (int i = 0; i < aList.Count; i++)        //Count属性确定元素实际个数

            {

                teacher my = (teacher)aList[i];      //如果数组中存放对象需要进行强制转换

                my.show();

            }

            Console.ReadLine();

        }

    }

    class teacher

    {

        public string name;

        public string worker;

        public teacher(string name,string worker)

        {

            this.name = name;

            this.worker = worker;

        }

        public void show()

        {

            Console.WriteLine("{0} {1}",name ,worker );

        }

    }

}

 

0 0
原创粉丝点击