首页 > 其他分享 >使⽤泛型和索引器实现集合类MyLis

使⽤泛型和索引器实现集合类MyLis

时间:2023-04-26 21:39:02浏览次数:34  
标签:count index myList int 索引 Add MyLis 泛型 data

  1 using System;
  2 
  3 namespace _01_调试和错误排查
  4 {
  5     class MyList<T>
  6     {
  7         private T[] data = new T[4];
  8         private int count = 0;
  9 
 10         //索引器
 11         public T this[int index]
 12         {
 13             get
 14             {
 15                 if (index < 0 || index >= count)
 16                 {
 17                     throw new ArgumentOutOfRangeException("索引参数超出范围");
 18                 }
 19                 return data[index];
 20             }
 21             set
 22             {
 23                 data[index] = value;
 24             }
 25         }
 26 
 27         //获取容量
 28         public int Capacity
 29         {
 30             get
 31             {
 32                 return data.Length;
 33             }
 34         }
 35 
 36         //获取长度
 37         public int Count
 38         {
 39             get
 40             {
 41                 return count;
 42             }
 43         }
 44 
 45         //添加元数
 46         public void Add(T value)
 47         {
 48             if (data.Length <= 0)
 49             {
 50                 data = new T[4];
 51             }
 52             if (data.Length == count)
 53             {
 54                 T[] _arr = new T[count * 2];
 55                 for (int i = 0; i < data.Length; i++)
 56                 {
 57                     _arr[i] = data[i];
 58                 }
 59                 data = _arr;
 60             }
 61             data[count] = value;
 62             count++;
 63         }
 64 
 65         //插入
 66         public void insert(int index, T item)
 67         {
 68             if (index < 0 || index > count)
 69             {
 70                 throw new ArgumentOutOfRangeException("索引参数超出范围");
 71             }
 72             for (int i = count - 1; i > index - 1; i--)
 73             {
 74                 data[i + 1] = data[i];
 75             }
 76             data[index] = item;
 77             count++;
 78         }
 79 
 80         //移除元素
 81         public void RemoveAt(int index)
 82         {
 83             if (index < 0 || index > count)
 84             {
 85                 throw new ArgumentOutOfRangeException("索引参数超出范围");
 86             }
 87             for (int i = index; i < count; i++)
 88             {
 89                 data[i] = data[i + 1];
 90             }
 91             count--;
 92         }
 93 
 94         //从前往后查找元素索引
 95         public int IndexOf(T item)
 96         {
 97             int temp = -1;
 98             for (int i = 0; i < count; i++)
 99             {
100                 if (item.Equals(data[i]))
101                 {
102                     temp = i;
103                     break;
104                 }
105             }
106             return temp;
107         }
108 
109         //从后往前查找元素索引
110         public int LastIndexOf(T item)
111         {
112             int temp = -1;
113             for (int i = count - 1; i >= 0; i--)
114             {
115                 if (item.Equals(data[i]))
116                 {
117                     temp = i;
118                     break;
119                 }
120             }
121             return temp;
122         }
123 
124         //排序
125         public void Sort()
126         {
127             Array.Sort(data, 0, count);
128         }
129     }
130 
131     public class Test
132     {
133         static void Main()
134         {
135             MyList<int> myList = new MyList<int>();
136 
137             //测试
138             myList.Add(11);
139             myList.Add(2);
140             myList.Add(32);
141             myList.Add(14);
142             myList.Add(43);
143             myList.Add(11);
144             myList.Add(6);
145             myList.Add(57);
146             myList.Add(81);
147             myList.Add(9);
148             myList.Add(100);
149             myList[2] = 1;
150             myList.insert(2, 24);
151             myList.RemoveAt(7);
152             Console.WriteLine(myList.IndexOf(11));
153             Console.WriteLine(myList.LastIndexOf(11));
154             Console.WriteLine("容量" + myList.Capacity);
155             Console.WriteLine("长度" + myList.Count);
156 
157             for (int i = 0; i < myList.Count; i++)
158             {
159                 Console.Write(myList[i] + " ");
160             }
161             myList.Sort();
162             Console.WriteLine();
163             for (int i = 0; i < myList.Count; i++)
164             {
165                 Console.Write(myList[i] + " ");
166             }
167         }
168     }
169 }

 

标签:count,index,myList,int,索引,Add,MyLis,泛型,data
From: https://www.cnblogs.com/zerobeyond/p/17357384.html

相关文章

  • Java泛型简单总结
    [code]Java泛型简单总结1)基本概念:泛型(GenericType或Generics)是对Java语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类。可以把类型参数看做是使用参数化类型时指定的类型的一个占位符,就像方法的形式参数是运行时传递的占位符一......
  • Three.js教程:顶点索引复用顶点数据
    推荐:将NSDT场景编辑器加入你3D工具链其他工具系列:NSDT简石数字孪生顶点索引复用顶点数据通过几何体BufferGeometry的顶点索引属性BufferGeometry.index可以设置几何体顶点索引数据,如果你有WebGL基础很容易理解顶点索引的概念,如果没有也没有关系,下面会通过一个简单的例子形象说......
  • 索引列表的制作,中文拼音排序
    业务上最近需要做一个选择人员的页面,右侧会有一个快速索引,样式如下:这个首先要把名字转拼音,然后取首字母,转大写,然后在新建的空对象里进行比对,如果有这个字母,就吧这条数据push进去,没有的话就在对象里创建该首字母的数组,再push进去,这样就形成了一个包含26个英文字母数组的对象结构......
  • MySQL索引详解
     DB哥MySQL高级教程-系统学习MySQL共149课时加我微信公众号免费学:DB哥文末有MySQL高级课程目录前言因为现在使用的mysql默认存储引擎是Innodb,所以本篇文章重点讲述Innodb下的索引,顺带简单讲述其他引擎。希望小伙伴们能通过这片文章对mysql的索引有更加清晰的认识,废话不多......
  • PGSQL 查询哪些表要索引,查表行数
    转自:(96条消息)PostgreSQLindexmonitor——监控哪些表需要创建索引_foucus、的博客-CSDN博客在数据库的使用过程中,可能某些表随着数据量的增大而因为没有索引仍旧使用的全表扫描,我们可以使用下列脚本来监控哪些大表上需要创建索引。1、监控哪些表需要创建索引SELECTr......
  • 【Mysql】复合主键的索引
    复合主键在where中使用查询的时候到底走不走索引呢?例如下表:createtableindex_test(aintnotnull,bintnotnull,cintnotnull,dintnull,primarykey(a,b,c));当执行以下SQL的时候到底走不走索引呢?SELECT*FROMindex_testWHERE......
  • 深入理解C#泛型:new与where关键字全解析
    C#泛型中new和where是重要的关键字,它们都可以用于约束泛型类型参数的限制;它们都用于提高代码的安全性和可用性,它们的作用在很大程度上提高了代码的可读性和可维护性。在这篇文章中,我们将一起了解泛型中的new和where,以及它们之间的区别。1.new关键字在C#泛型中,new关键字被用于指......
  • 关于ABAP索引
    1、什么是索引如果把数据库表看做一本书,索引就可以看做书的检索目录。目录中包含书中的大小标题(部分字段数据),并且有对应的数据表条目的页码(指针),可以快速的访问数据库表中对应行的所有字段内容一个表中包含一个主索引和多个二级索引主索引:是系统根据表关键字自动创建的,用户不需要创......
  • edge如何设置搜索栏默认搜索引擎
    首先,找到“隐私、搜索和服务”下的“地址栏和搜索”: 点击“地址栏中使用的搜索引擎”右侧的多选框选择引擎: ......
  • MySQL查看索引
    查看一张指定表的索引信息点击查看代码showindexfrom tablename;查询某个数据库(table_schema)的全部表索引点击查看代码--排除主键索引selectTABLE_NAME,INDEX_NAME,GROUP_CONCAT(COLUMN_NAME)asINDEX_COLUMNfrominformation_schema.statisticswheretable_......