using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ArrayList
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { "元素1", "元素2", "元素3", "元素4", "元素5", "元素6" };//定义字符串数组
            ArrayList a1 = new ArrayList(str);
            int i = a1.IndexOf("元素3");//得到“元素3”的第一次出现索引位置
            Console.WriteLine("元素3在集合中的位置是:" + i);//输出元素3的所以位置
            i = a1.LastIndexOf("元素5");//得到“元素5”的最后一次出现的索引位置
            Console.WriteLine("元素5在集合中的位置是:" + i);
            int j = a1.BinarySeach("元素3");
            if (j > 0)
                Console.WriteLine("元素3在集合中的位置是:" + j);
            else
                Console.WriteLine("没找到元素3!");
            Console.ReadKey();
        }
    }
}这个沉痼怎么运行不了啊?
 怎么创建ArrayList啊?

解决方案 »

  1.   

    ArrayList的构造函数没有直接添加元素的方法,应该先new一个,然后用add方法将元素加到arrayList中去
      

  2.   

    ArrayList位于System.Collections.Generic;命名空间下。ArrayList list = new ArrayList();
    有一个构造函数可以指定该集合的容量。
    但是ArrayList的容量是可以根据需求自动扩充的。所以这个参数可写也可不写。ArrayList详解