C#中利用ArrayList来对索引器访问越界情况进行内容的扩充处理

对于http://www.cnblogs.com/lihaozy/archive/2010/10/28/1863469.html一文中的用索引器对某一对象中的内置数组访问时,会出现数组越界的情况,我们使用了ArrayList来代替该对象所属类中的数组。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    public class MyPreviousExp
    {
        private ArrayList myCompanies2 = new ArrayList(10);

         //index creation
        public string this[int index]{
                      get
                      {
                          if (index < 0 || index >= 6)
                              return "null";
                          else
                              return myCompanies2[index].ToString();
                      } 
                      set
                      {
                          if (!(index < 0 || index >= 10))//初始时只有10的capacity
                          {
                              myCompanies2.Insert(index, value);
                              myCompanies2.RemoveAt(index + 1);
                          }
                          else if (index >= 10)//但是如果超过了10,我们也可以动态的变更大小
                          {
                              if (index < myCompanies2.Count)
                              {
                                  myCompanies2.Insert(index, value);
                                  myCompanies2.RemoveAt(index + 1);
                              }
                              else
                              {
                                  int temp = index - (myCompanies2.Count - 1);
                                  for (int i = 0; i < temp; i++)
                                  {
                                      myCompanies2.Add("");//这里如果添加的是null的话,那么后面在输出Console.WriteLine(" My Companies{0} : {1} ", i, indexerObj[i].ToString());的时候就会出问题,因为null不是一个object,所以就没法转换成string
                                  }
                                  myCompanies2.Insert(index, value);
                                  myCompanies2.RemoveAt(index + 1);
                              }
                          }
                      }
        }
        
        public static void Main()
        {
            try
            {
                MyPreviousExp indexerObj = new MyPreviousExp();
                for (int i = 0; i < 10; i++)
                {
                    indexerObj.myCompanies2.Add(i.ToString());//先要对Arraylist中的东西插入元素!
                }

                indexerObj[1] = "AMS";
                indexerObj[3] = "HCL";
                indexerObj[5] = "ACC";
                indexerObj[8] = "OFW";
                indexerObj[15] = "CFG";//这里插入的元素的index是15,所以在MyPreviousExpde set()方法中,就对indexOutOfRange做了处理,先添加相对应个数的""空字符串,然后再在相对应的位置上给该元素赋值

                for (int i = 0; i < indexerObj.myCompanies2.Count; i++)
                {
                    Console.WriteLine(" My Companies{0} : {1} ", i, indexerObj[i].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

 

 

输出为:

 

 My Companies0 : 0
 My Companies1 : AMS
 My Companies2 : 2
 My Companies3 : HCL
 My Companies4 : 4
 My Companies5 : ACC
 My Companies6 : null
 My Companies7 : null
 My Companies8 : null
 My Companies9 : null
 My Companies10 : null
 My Companies11 : null
 My Companies12 : null
 My Companies13 : null
 My Companies14 : null
 My Companies15 : null

 

 

可以看到,类初始的时候申请了大小为10的空间,但是因为是ArrayList,通过在利用索引器对myCompanies2中的ArrayList赋值时,发生越界的情况,进行了add()的处理,来补充够ArrayList中元素的个数。从而实现了扩充到16大小的结果(见上面的结果)(后面都是null是因为在get的定义中,对于大于等于6的索引,是不返回真实值的,而是返回null)。

posted on 2010-10-28 21:23  kkmm  阅读(1554)  评论(0编辑  收藏  举报