using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CustomizedList {
class MyList<T> where T:IComparable
{
private T[] array;
private int count=0;//表示当前添加的元素的个数 public MyList(int size)
{
if (size >= 0)
{
array = new T[size];
}
} public MyList()
{
array = new T[0];
} public int Capacity
{
get { return array.Length; }
} public int Count
{
get { return count; }
} public void Add(T item )
{
if (Count == Capacity) //判断元素个数跟列表容量大小是否一样大,如果一样大,说明数组容量不用,需要创建新的数组
{
if (Capacity == 0)
{
array = new T[4];//当数组长度为0的时候,创建一个长度为4的数组
}
else
{
var newArray = new T[Capacity*2];//当长度不为0的时候,我们创建一个长度为原来2倍的数组
Array.Copy(array,newArray,Count);//把旧数组中的元素复制到新的数组中 , 复制前count个 array-->newArray
array = newArray;
}
}
array[Count] = item;
count++;//元素个数自增
} public T GetItem(int index)
{
if (index >= 0 && index <= count - 1)
{
return array[index];
}
else
{
//Console.WriteLine("索引超出了范围");
throw new Exception("索引超出了范围");
}
} public T this[int index]
{
get//当我们通过索引器取值的时候,会调用get块
{ return GetItem(index); }
set//当我们通过索引器设置值的时候,会调用set块
{
if (index >= 0 && index <= count - 1)
{
array[index] = value;
} else {
//Console.WriteLine("索引超出了范围");
throw new Exception("索引超出了范围");
}
}
} public void Insert(int index, T item)
{
if (index >= 0 && index <= count - 1)
{
if (Count == Capacity)//容量不够 进行扩容
{
var newArray = new T[Capacity*2];
Array.Copy(array,newArray,count);
array = newArray;//newArray被GC机制回收
}
for (int i = count-1; i >=index; i--)
{
array[i + 1] = array[i];//把i位置的值放在后面,就是向后移动一个单位 }
array[index] = item;
count++;
}
else
{
throw new Exception("所以超出范围");
}
} public void RemoveAt(int index)
{
if (index >= 0 && index <= count - 1)
{
for (int i = index + 1; i < count; i++)
{
array[i - 1] = array[i];
}
count--;
}
else
{
throw new Exception("索引超出范围");
}
} public int IndexOf(T item)
{
for (int i = 0; i < count; i++)
{
if (array[i].Equals(item))
{
return i;
}
}
return -1;
} public int LastIndexOf(T item)
{
for (int i = Count-1; i >=0; i--) {
if (array[i].Equals(item)) {
return i;
}
}
return -1;
} public void Sort()
{
for (int j = 0; j < Count-1; j++)
{
for (int i = 0; i < Count - 1 - j; i++) {
if (array[i].CompareTo(array[i + 1]) > 0) {
T temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
} }
} //测试
class Program {
static void Main(string[] args) { MyList<int> mylist = new MyList<int>();
mylist.Add(234);
mylist.Add(14);
mylist.Add(24);
mylist.Add(24);
mylist.Add(90);
mylist.Add(274);
//mylist[index]
mylist.Insert(1,2);
//mylist.RemoveAt(-1);
mylist[0] = 100;//通过索引器 设置值
for (int i = 0; i < mylist.Count; i++)
{
//Console.WriteLine(mylist.GetItem(i));
Console.WriteLine(mylist[i]);//通过索引器 取值
}
Console.WriteLine(mylist.IndexOf(24));
Console.WriteLine(mylist.LastIndexOf(24));
mylist.Sort();
Console.WriteLine();
for (int i = 0; i < mylist.Count; i++) {
//Console.WriteLine(mylist.GetItem(i));
Console.WriteLine(mylist[i]);//通过索引器 取值
}
Console.ReadKey();
}
}
}

C#实现自定义列表的更多相关文章

  1. SharePoint 2013开发入门探索(一)- 自定义列表

    在SharePoint 2013中创建自定义列表的方式有很多,在网站内容页面添加应用程序就可以创建(站点内容-〉 您的应用程序),也可以通过SharePoint Designer 2013创建,而本文 ...

  2. [安卓] 16、ListView和GridView结合显示单元实现自定义列表显示效果

    List在各种手机应用中都有体现,是安卓UI设计的必修课. 本文将介绍在开发中如何利用ListView和GridView设计自定义列表. 下面分别是用ListView和GridView做的效果: 上面 ...

  3. 自定义列表dl的使用原因和场合

    为什么要使用自定义列表? dl和ol, ul的区别? 要正确理解dl的意图, 理解 dl的 "语义" ! 才能知道为什么要使用dl, 以及在什么时候/ 什么情况下使用 dl? dl ...

  4. 每日学习心得:SharePoint 2013 自定义列表项添加Callout菜单项、文档关注、SharePoint服务端对象模型查询

    前言: 前一段时间一直都比较忙,没有什么时间进行总结,刚好节前项目上线,同时趁着放假可以好好的对之前遇到的一些问题进行总结.主要内容有使用SharePoint服务端对象模型进行查询.为SharePoi ...

  5. 我教女朋友学编程html系列(7)—Html无序列表、自定义列表、有序列表及常用例子

    昨天写的那篇文章<我教女朋友学编程Html系列(6)—Html常用表单控件>,基本上有1000人左右看了,那边文章是我站在前人的肩膀上修改来的,添加了截图和说明,合并了例子,使之更容易被初 ...

  6. sharepoint2010 创建自定义列表

    转:http://boke.25k5.com/kan77298.html 如何创建自定义列表 首先了解创建自定义列表中涉及到的几个名词:栏.内容类型. ①栏:栏即列.字段(Field),MSDN中给出 ...

  7. Swift基础--使用TableViewController自定义列表

    首先建立一个swift项目,把storyboard的内容删掉,添加一个 Navigation Controller,然后设置storyboard对应界面的class,在Navigation Contr ...

  8. sharepoint 2010 使用自定义列表模版创建列表(2)

    前面用的方法是通过界面上操作,根据自定义模版,创建的列表.sharepoint 2010 使用自定义列表模版创建列表(1) 这里顺便记录多另一种方法,通过程序来创建. ---------------- ...

  9. 帝国cms7.2自定义列表建立tag效果 代码 教程

    统计记录:(如:select count(*) as total from phome_ecms_news where classid=1 and checked=1) 注:这句SQL的意思是查找统计 ...

  10. sharepoint 2010 在自定义列表的字段上增加功能菜单

    sharepoint 2010 在自定义列表的字段上增加功能菜单方法 打开sharepoint designer 2010,找到需要修改的视图页面,例如allitem.aspx,编辑这个页面,点击高级 ...

随机推荐

  1. fullpage.js禁止滚动

    http://www.wenjiangs.com/doc/fullpage-method 转载于:https://www.cnblogs.com/hzz-/p/8268771.html

  2. Python(Redis 中 String/List/Hash 类型数据操作)

    1.下载 redis 模块 pip install redis 2.redis 数据库两种连接方式 简单连接 decode_responses=True,写入和读取的键值对中的 value 为 str ...

  3. startUML简单使用教程

    最近在startUML上画图搞得我一个头两个大,找了一些比较实用的小方法分享给大家. 类图是可视化地表现类的属性和方法,以及类与类之间的关系. 1.类 a.类 普通实体类,它有对应的属性和方法: 双击 ...

  4. Java笔记(day18-19)

    泛型: jdk1.5出现的安全机制. 好处: 1,将运行时期的问题ClassCastException转到了编译时期. 2,避免了强制转换的麻烦. <>:当操作的引用数据类型不确定的时候. ...

  5. Programming Languages_04 Deferred Substitution

    Deferred Substitution 在执行出现with时,利用"substitution",每次with的出现,它都绕着整个body置换.这一方式是由F1WAE到env再到 ...

  6. Hadoop入门学习笔记-第一天 (HDFS:分布式存储系统简单集群)

    准备工作: 1.安装VMware Workstation Pro 2.新建三个虚拟机,安装centOS7.0 版本不限 配置工作: 1.准备三台服务器(nameNode10.dataNode20.da ...

  7. 线段树 B数据结构 牛客练习赛28

    链接:https://ac.nowcoder.com/acm/contest/200/B来源:牛客网 题目描述 qn姐姐最好了~     qn姐姐给你了一个长度为n的序列还有m次操作让你玩,     ...

  8. Linux(Ubuntu) MySQL数据库安装与卸载

    安装 修改远程访问 卸载 安装 首先检查系统中是否已经安装了MySQL sudo netstat -tap | grep mysql 没有显示已安装结果,则没有安装 如若已安装,可以选择删除.(删除方 ...

  9. HDU-6351 Beautiful Now 全排列暴力

    Beautiful Now 题意 给出一个最大为10^9的数字n,以及一个k,你最多交换n中任意两个位置的数字k次,问形成的最大数字和最小数字. 思路 看到这题,我靠这题暴力交换一下,不难啊,咋没人做 ...

  10. nginx源码安装方法

    nginx源码安装方法 安装方法如下 1.安装nginx必要的源码依赖软件包. yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zli ...