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. Struts2深入之动态调用Action

    使用过Struts2的小伙伴们应该知道当我们的action的方法过多是如果需要通过Struts2框架进行运行,我们就必须在Struts2的配置文件Struts2.xml文件中配置多个action属性标 ...

  2. lua 发送http请求

    lua发送http请求,luajit默认没有http.lua库,需要下载并存放到luajit对应目录. 一.下载http.lua和http_headers.lua库 参考:https://www.zi ...

  3. How to get binary string from ArrayBuffer?

    https://stackoverflow.com/questions/16363419/how-to-get-binary-string-from-arraybuffer https://stack ...

  4. CodeForces - 1102B Array K-Coloring

    B. Array K-Coloring time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  5. P1518 两只塔姆沃斯牛 The Tamworth Two(简单的搜索题)

    题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为(牛和John). 追击在10x10的平面网格内进行.一个格子可以是: 一个障碍物, 两头牛(它们总 ...

  6. json格式总结

    json格式分为两种: 1.键值对: 2.数组:其中元素可以是字符串.数字.数组,还可以相互嵌套 其中图片来源于:https://blog.csdn.net/huapenguag/article/de ...

  7. Linux安装Git-两种方式详细教程)

    一.Git介绍 Git --- The stupid content tracker, 傻瓜内容跟踪器.Linus Torvalds 是这样给我们介绍 Git 的. Git 是用于 Linux内核开发 ...

  8. nginx代理路径配置总结

    一.发现问题 配置nginx代理的时候,发现location配置的路径和代理的上下文路径的组合不同,服务端接收到的uri的路径不同,导致了controller的RequestMapping匹配出现问题 ...

  9. 报错:Maven创建An internal error occurred during: "Retrieving archetypes:". Java heap space

    在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archetypes:". J ...

  10. Cookie什么?Cookie和Session防御怎么做?

    Cookie什么?Cookie和Session防御怎么做? Cookie的概念 Cookie,复数形态Cookies,中文名称为小型文本文件.指某些网站为了辨别用户身份.进行session跟踪而储存在 ...