C#复习之索引器

知识点一:索引器基本概念

基本概念:

让对象可以像数组一样通过索引访问其中元素,使程序看起来更直观,更容易编写

知识点二:索引器语法

//value代表传入的值

知识点三:索引器的使用

知识点四:索引器中可以写逻辑

知识点五:索引器可以重载

重载的概念是---函数名相同 参数类型、数量、顺序不同

可以把this看成函数名

总结:

索引器对于我们来说的主要作用

可以让我们以中括号的形式范围自定义类中的元素 规则自己定 访问时和数组一样

比较适用于 在类中有数组变量时使用 可以方便的访问和进行逻辑处理

固定写法

访问修饰符 返回值 this[参数列表]

get和set语句块

可以重载
注意:结构体里面也是支持索引器

练习题:自定义一个整形数组类,该类中有一个整形数组变量,为它封装增删查改的方法

cpp 复制代码
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 索引器
{
    internal class Program
    {
        static void Main(string[] args)
        {
           IntArray array = new IntArray();
            array.Add(100);
            array.Add(200);
            array.Add(300);
            array.Add(400);
            array.Add(500);
            Console.WriteLine(array[0]);
            array.RemoveAt(0);
            Console.WriteLine(array[1]);
            array.Remove(200);
            Console.WriteLine(array[1]);
            Console.WriteLine(array[6]);
            Console.WriteLine(array.Length);
        }
    }

    class IntArray
    {
        private int[] array;

        private int capacity;//房间容量
        private int length;//长度(当前放了几个房间)

        public IntArray()
        {
            capacity = 5;
            length = 0;
            array = new int[capacity];
        }
        //增
        public void Add(int value)
        {
            //如果要增加就涉及扩容
            //扩容就涉及"搬家"
            if (length<capacity)
            {
                array[length] = value;
                ++length;
            }
            //扩容
            else
            {
                capacity *= 2;
                //新房子
                int[] tempArray =new int[capacity];
                //老东西放进新房子里
                for (int i = 0; i < array.Length; i++)
                {
                    tempArray[i] = array[i];
                }
                //老的房子地址 指向新房子的地址
                array = tempArray;

                //往后面放
                array[length] = value;
                ++length;
            }
        }
        //删
        public void Remove(int value)
        {
            //找到传入值在哪个位置
            for (int i = 0; i < length; i++)
            {
                if (array[i] == value)
                {
                    RemoveAt(i);
                    return;
                }
            }
            Console.WriteLine("没有在数组中找到{0}", value);
        }
        public void RemoveAt(int index)
        {
            if (index>length-1)
            {
                Console.WriteLine("当前数组只有{0},你越界了", length);
                return;
            }
            for (int i = index; i < length-1; i++)
            {
                array[i] = array[i + 1];
            }
            --length;
        }
        //查改
        public int this[int index]
        {
            get {
                if (index>=length||index<0)
                {
                    Console.WriteLine("越界");
                    return 0;
                }
                return array[index]; }
            set {
                if (index>=length||index<0)
                {
                    Console.WriteLine("越界");
                }
                array[index] = value;}
        }
        public int Length
        {
            get { return length; }
        }
    }
}
相关推荐
csdn_aspnet2 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
paeamecium4 小时前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
Book思议-4 小时前
【数据结构】栈与队列全方位对比 + C 语言完整实现
c语言·数据结构·算法··队列
是苏浙5 小时前
初识集合框架
java·数据结构
如何原谅奋力过但无声6 小时前
【chap11-动态规划(上 - 基础题目&背包问题)】用Python3刷《代码随想录》
数据结构·python·算法·动态规划
武藤一雄7 小时前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore
小王C语言7 小时前
【基础IO】————简单设计一下libc库
前端·数据结构·算法
_日拱一卒8 小时前
LeetCode:滑动窗口的最大值
数据结构·算法·leetcode
格林威8 小时前
Baumer相机锂电池极片裁切毛刺检测:防止内部短路的 5 个核心方法,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·视觉检测
老约家的可汗8 小时前
list 容器详解:基本介绍与常见使用
c语言·数据结构·c++·list