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; }
        }
    }
}
相关推荐
刘欣的博客11 小时前
C# CS架构程序发版升级的走数据库方案
c#·单文件升级自己的方式
不知名XL12 小时前
day50 单调栈
数据结构·算法·leetcode
Yorlen_Zhang13 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝19113 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话13 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
cpp_250114 小时前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_250114 小时前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷
czhc114007566315 小时前
通信 28
c#
季明洵15 小时前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
only-qi15 小时前
leetcode19. 删除链表的倒数第N个节点
数据结构·链表