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; }
        }
    }
}
相关推荐
im_AMBER5 分钟前
数据结构 11 图
数据结构·笔记·学习·图论
czhc114007566321 分钟前
C#1114 枚举
开发语言·c#
xiaoye-duck1 小时前
数据结构之二叉树-链式结构(上)
数据结构
曹牧1 小时前
C#中,GetValueOrDefault方法
c#
Doro再努力1 小时前
2025_11_14洛谷【入门1】数据结构刷题小结
前端·数据结构·算法
cs麦子2 小时前
C语言--详解--指针--下
c语言·数据结构·算法
flashlight_hi2 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
oioihoii3 小时前
C++中有双向映射数据结构吗?Key和Value能否双向查找?
数据结构·c++·算法
小欣加油3 小时前
leetcode 2536 子矩阵元素加1
数据结构·c++·算法·leetcode·矩阵
hnjzsyjyj4 小时前
AcWing 3595:二叉排序树 ← BST
数据结构·bst·二叉排序树