zoukankan      html  css  js  c++  java
  • 黑马程序员-集合和索引器

    索引器

    为了方便将类、结构或接口当做数组来使用。

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

    定义语法

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

    {

      //get或set方法体

    }

    索引本质就是属性

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

    集合具有很强的通用性(方法名应该记住)

    增加

     Add、AddRange

    移除

     Remove、RemoveAt

    清空

     Clear

    排序

     Sort、Reverse

    查找

     IndexOf、LastIndexOf

    判存

     Contains

    集合的总数

     Count属性

    浅复制

     Clone

     

    索引

    集合也可以通过“下标”来访问,叫做索引

     

    namespace Indexer

    {

        class Program

        {

            static void Main(string[] args)

            {

                MyCollection mc = new MyCollection();

                int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

                // 添加的功能

                mc.Add(100);

                mc.AddRange(nums);

                // 插入,在3位置上添加一个100

                mc.Insert(3, 100);

                // 移除

                //mc.Remove(100);

                mc.RemoveAt(2);

     

                // 清空

                //mc.Clear();

                // 读取位置为3的数据

                Console.WriteLine(mc[2]);

     

                Console.ReadKey();

            }

        }

     

        // 咱们手动写一个集合

        class MyCollection

        {

            //int[] nums;

            ArrayList al;

            // 用来测试的方法

            private void Test()

            {

                this.Clear();

                // this在类的内部,表示当前实例

                //this[3];

            }

     

            //索引

            //索引的本质是属性;属性的本质是方法。

            public object this[int index]

            {

                get { return al[index]; }

                set { al[index] = value; }

            }

     

            public MyCollection()

            {

                al = new ArrayList();

            }

     

            // Add方法

            // 最不好的办法

            #region 不好的方法

            //public int Add(int num)

            //{

            //    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;

            //        nums = temp;

            //    }

            //    return nums.Length + 1;

            //}

            #endregion

     

            public int Add(object o)

            {

                return al.Add(0);

            }

            public void AddRange(ICollection ic)

            {

                al.AddRange(ic);

            }

     

            public void Remove(object o)

            {

                al.Remove(o);

            }

            public void RemoveAt(int index)

            {

                al.RemoveAt(index);

            }

     

            public void Clear()

            {

                al.Clear();

            }

     

            public void Insert(int index, object o)

            {

                al.Insert(index, o);

            }

        }

    }

     

  • 相关阅读:
    修改sharepoint列表样式
    Zend Server 安装与配置图文教程
    安装zendstudio和破解方法及配置svn
    C#索引器
    曝光尚德机构亲身经历请大家不要上当
    不支持PowerShell 2.0版本(don't support PowerShell version 2.0. )
    SharePoint Project Server List 列表CURD操作使用rest api接口
    解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题
    Project Server2016升级安装问题项目中心无法显示
    Project Server 2016 RestAPI调用测试
  • 原文地址:https://www.cnblogs.com/xsj891107/p/3664048.html
Copyright © 2011-2022 走看看