Arraylist本来就有索引啊。如果你希望你的Collection类也能象ArrayList那样直接通过[]访问的话,你可以实现IList接口,在Item属性中做相应操作。

解决方案 »

  1.   

    其实,ArrayList已经编制索引,如果移除某处的元素,那么这个已移除元素之后的元素的索引都要减一:ArrayList list = new ArrayList();
    list.Add("aa");
    list.Add("bb");
    list.Add("cc");
    object obja = list[0];
    object objc = list[2];//"cc"的索引是2
    list.RemoveAt(1);
    object objc2 = list[1];//因为移掉第二个元素,所以二个以后的元素的索引都要减一,也就是说,如果要取得"cc"这个元素,索引由原来的2变为1。
      

  2.   

    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemCollectionsArrayListClassRemoveAtTopic.htm这个地址怎么访问?是直接粘贴到地址栏吗?
    我怎么访问不了呢?
      

  3.   

    输入到 MSDN中的地址栏即可!
      

  4.   

    非常感谢  IceboundRock() 和 xixigongzhu(夕夕公主):请问你们用到ArrayList的地方在那儿,就是说说ArrayList的具体应用例子是否能给我看看?MSDN 的太简单了!
      

  5.   

    需要用动态数组的地方都可以使用,需要一个类似数组的东东,但不能确定其个数,就用ArrayList吧。比如如果需要维护一个控件的列表,
    ArrayList controlList = new ArrayList();
    controlList.Add(textBox1);
    controlList.Add(textBox2);
    controlList.Add(button1);
    if ( (int)button2.Tag == 2 )
      controlList.Add(button2);for(int i=0; i < controlList.Count; i++)
    {
      ...
    }
      

  6.   

    做个集合类,类中有这个集合的索引,其中数据都放在一个arraylist中。当增加、删除的时候,就用arraylist的方法,但这个索引只是维护集合的数量和当前项用。
      

  7.   

    liduke(天下有雪) ,你那有ArrayList的例子吗?
      

  8.   

    using System;
    using System.Collections;
    namespace Property
    {
    /// <summary>
    /// PropertyCollection 的摘要说明。
    /// </summary>
    public class PropertyCollection:CollectionWithEvent
    {
    public PropertyCollection()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public PropertyT this[int index]
    {
    get
    {
    if( index < this.Count)
    {
    return base.List[index] as PropertyT;

    }
    else
    {
    return null;
    }

    }
    }

    public PropertyT Add( PropertyT value)
    {
    base.List.Add( value as object);
    return value;
    }
    }
    }
      

  9.   

    CollectionWithEvent类是从CollectionBase中继承过来,基类中实现了ILIST等接口,有个ArrayList类型的变量List