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; }
        }
    }
}
相关推荐
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹5 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
m0_547486665 天前
《数据结构教程》全套 PPT课件2026
数据结构
kybs19915 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
tryxr6 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_36 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Polevne6 天前
C# SFTP客户端实现文件传输
c#·ssh
samble6 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
All for pursuit.6 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.6 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode