ArrayList集合与索引器及Hashtable

class Program
    {
        static void Main(string[] args)
        {
            #region 引子
            int[] num = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //把第三个位置的数字删掉
            for (int i = 3; i < num.Length - 1; i++)
            {
                num[i] = num[i + 1];
            }
            //在位置为3的地方插入一个数字,数组的长度是不可变的,所以实现起来很麻烦
            //由此引入集合 
            #endregion

            ArrayList a1 = new ArrayList();//集合体具有很强的通用性(方法名应该记住)
            //增加
            a1.Add(0);
            a1.Add(num);//调试.此时a1.count=2;
            a1.AddRange(num);//调试,此时a1.count=12相当于
            //for (int i = 0; i < num.Length;i++ )
            //{
            //    a1.Add(num[i]);
            //}
            
            a1.AddRange(a1); //count 为24
            a1.RemoveAt(3);//count为23,去掉了位置为3的数,将后面的书往前挪一位
            a1.Remove(3);//移除第一次出现的那个3
            a1.Clear();//清除

                //排序
            int[] nums = new int[] { 2, 5, 1, 2, 4, 56, 78, 67, 23, 45, 123 };
            a1.AddRange(nums);
            a1.Sort();
            a1.Reverse();

                //浅复制
            ArrayList a2 = (ArrayList)a1.Clone();
            a1.Sort();//此时a1重新排序从小到大,a2还是从大到小
              
            //索引集合也可以通过下标访问 
            for (int i = 0; i < a1.Count/2;i++ )
            {
                object obj;
                obj = a1[i];
                a1[i] = a1[a1.Count - 1 - i];
                a1[a1.Count - 1 - i] = obj;
            }

手动写一个集合类:

class Program
    {
        static void Main(string[] args)
        {
            MyCollection mc = new MyCollection();
            int[] nums = new int[] { 2, 3, 4, 6 };
            //添加功能
            mc.Add(100);
            mc.AddRange(nums);
            //插入功能
            mc.Insert(3, 200);
            //移除功能
            mc.Remove(100);
            //清空
            mc.Clear();
            //读取位置为3的位置mc[3],所以要添加索引器
            mc.AddRange(nums);
            Console.WriteLine(mc[3]);
            Console.ReadKey();
        }
    }
    class MyCollection
    {
       // int[] nums;
        ArrayList a;
        public MyCollection()
        {
            a = new ArrayList();
        }
        public int Add(object obj)
        {
            #region 繁琐的方法,不推荐,消耗内存
            //if (nums == null)
            //{
            //    nums = new int[] { num };
            //}
            //else
            //{
            //    int[] temp = new int[nums.Length + 1];
            //    for (int i = 0; i < nums.Length; i++)
            //    {
            //        temp[i] = nums[i];
            //    }
            //    temp[temp.Length - 1] = num;
            //}
            //return nums.Length + 1; 
            #endregion
            return a.Add(obj);
        }
        public void AddRange(ICollection ic)
        {
            a.AddRange(ic);
        }

        public void Remove(object obj)
        {
            a.Remove(obj);
        }
        public void RemoveAt(int index)
        {
            a.RemoveAt(index);
        }

        public void Clear()
        {
            a.Clear();
        }
        public void Insert(int index, object obj)
        {
            a.Insert(index,obj);
        }
        public object this[int index]
        {
            get { return a[index]; }
            set { a[index] = value; }
        }
    }

通过上述例子,我们得知:

■索引器是为了方便将类,结构或接口当做数组来使用

■索引器用来封装内部集合或数组

■定于语法

  [访问修饰符]返回类型 this[索引类型 索引名]

  {

    //set或get方法体

  }

■索引的本质就是属性

■利用索引可以用key得到项,亦可用项得到序号

class Program
    {
        //如果不是自己写集合,那么就不需要写索引器
        //索引器是方便访问的一个封装
        // ArrayList是弱类型,Object不具备特殊性
        // 索引器不单单只用于集合,还能用于其他构造类型
        static void Main(string[] args)
        {
            //写一个存放Person对象的集合
            //不用自己的类型集合,使用ArrayList
            #region 繁琐代码使用ArrayList
            ArrayList a = new ArrayList();
            a.Add(new Person("翟群", 18, ''));
            a.Add(new Person("何雄军", 24, ''));
            //现在让他们说话
            for (int i = 0; i < a.Count; i++)
            {
                ((Person)a[i]).SayHello();
            }
            Console.ReadKey(); 
            #endregion

            MyCollection mc = new MyCollection();
            mc.Add(new Person("翟群", 18, ''));
            mc.Add(new Person("何雄军", 24, ''));
            for (int i = 0; i < mc.Count;i++ )
            {
                mc[i].SayHello();
            }
            Console.ReadKey();
        }
    }

 索引器在其他构造类型中

class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            string s = t[9, "+"];
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
    class Test
    {
        int num = 10;
        string str = "+";
        public string this[int num, string str]
        {
           get
            {
                string temp;
                if (this.num == num && this.str == str)
                {
                    temp = "存在该数字和字符串";
                }
                else if (this.num == num)
                {
                    temp = "存在该数字";
                }
                else if (this.str == str)
                {
                    temp = "存在字符串";
                }
                else
                {
                    temp = "不存在";
                }
                return temp;
            }
 
        }

 Hashtable:散列集合

 class Program
    {
        static void Main(string[] args)
        {
            Hashtable th = new Hashtable();
            th.Add("何雄军", 24);
            th.Add("翟群", 18);
            if (th.ContainsKey("何雄军"))
            {
                Console.WriteLine(th["何雄军"]);
            }
            else
            {
                Console.WriteLine("没有找到");
            }
            //如何遍历? for循环不可以,只能用foreach
            foreach (DictionaryEntry item in th)//设个断点,看看item是什么类型 再将var改成对应的类型 不建议常用类型推断var
            {
                Console.WriteLine(item.Key+""+item.Value);
            }
            foreach(string temp in th.Keys)
            {
                Console.WriteLine(temp);
            }
            //想加入一个名字叫做"何雄军"的人,可以吗
            //th.Add("何雄军", 20);//报错,因为index若是重复 ,怎么查询
        }

 

   int[] values = { 3, 3, 3, 4, 5, 8, 9, 9 };
            Hashtable h = new Hashtable();
            int count = 0;
            foreach(int i in values)
            {
                if (!h.ContainsKey(i))
                {
                    h.Add(i,"");
                    count++;
                }
            }
            Console.WriteLine(count);
            Console.ReadKey();

 

posted @ 2013-12-17 20:28  我叫小菜  阅读(445)  评论(0编辑  收藏  举报