public class MyList<T> where T : IComparable
{ private T[] array;
private int count;
public MyList(int size)
{
if (size >= )
{
array = new T[size];
} }
public MyList()
{
array = new T[];
}
public int Capacity
{
get
{
return array.Length;
}
}
public int Count
{
get { return count; }
}
public void Add(T a)
{
if (Count == Capacity)
{
if (Capacity == )
{
array = new T[];
}
else
{
var newarray = new T[Capacity * ];
Array.Copy(array, newarray, count);
array = newarray;
}
}
array[count] = a;
count++;
}
public T GetItem(int index)
{
if (index >= && index <= count - )
{
return array[index];
}
else
{
throw new Exception("超出索引");
}
}
public T this[int index]
{
get
{
return GetItem(index);
}
set
{ if (index >= && index <= count - )
{
array[index] = value;
}
else
{
throw new Exception("超出索引");
}
}
}
public void Insert(int a, T t)
{
if (a >= && a <= Count - )
{
if (count == Capacity)
{
var newarray = new T[Capacity * ];
Array.Copy(array, newarray, count);
array = newarray;
}
else
{
for (int i = count - ; i >= a; i--)
{
array[i + ] = array[i];
}
array[a] = t;
count++;
}
}
}
public void RemoveAt(int a)
{
if (a >= && a <= Count - )
{
for (int i = a + ; i <= count - ; i++)
{
array[i - ] = array[i];
}
count--;
}
}
public int IndexOf(T t)
{
for (int i = ; i <= count - ; i++)
{
if (array[i].Equals(t))
{
return i;
}
}
return -;
}
public int LastIndexOf(T t)
{
for (int i = count - ; i >= ; i--)
{
if (array[i].Equals(t))
{
return i;
}
}
return -;
}
public void Sort()
{
for (int j = ; j < count - ; j++)
{ for (int i = ; i < count - - j; i++)
{
if (array[i].CompareTo(array[i + ]) > )
{
T tamp = array[i];
array[i] = array[i + ];
array[i + ] = tamp;
}
}
}
}
}

泛型列表

【jq】c#零基础学习之路(5)自己编写简单的Mylist<T>的更多相关文章

  1. 【jq】c#零基础学习之路(1)Hello World!

    从今天起我会持续发表,这个就是一个日记型的,学习编程是枯燥的,况且我们还是零基础. 学前准备 1.编译环境 vs2010.vs2012.vs2015...(本人用的是vs2010旗舰版).vs2010 ...

  2. 【jq】c#零基础学习之路(4)抽象类和密封

    一.抽象类 1.抽象类不能被实例化 2.抽象类方法必需要实现 3.如何类中函数为抽象函数,其类也需要定义成抽象类 4.关键字 abstract ,函数重写 override. 二.密封类 1.密封类不 ...

  3. 【jq】c#零基础学习之路(3)继承和虚方法

    c#只能继承一个基类和多个接口(0+) 父类:Human: class Human { public virtual Move() { Console.WriteLine("Human的虚方 ...

  4. 【jq】c#零基础学习之路(2)循环和分支

    一.循环语句 1).do { //循环体,先运行一次. } while (true); 2). while (true) { //循环体 } 3). for (int i = 0; i < le ...

  5. python 零基础学习之路 02-python入门

    不知不觉学习python已经两个月了,从一开始不知道如何对print的格式化,到现在可以手撸orm,这期间真的是 一个神奇的过程.为了巩固自己的基础知识,为后面的拓展埋下更好的伏笔,此文当以导师的博客 ...

  6. Android 零基础学习之路

    第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正則表達式. 3.面向对象的抽象.封装,继承,多态.类与对象.对象初始化 ...

  7. salesforce零基础学习(七十九)简单排序浅谈 篇一

    我们在程序中经常需要对数据列表进行排序,有时候使用SOQL的order by 不一定能完全符合需求,需要对数据进行排序,排序可以有多种方式,不同的方式针对不同的场景.篇一只是简单的描述一下选择排序,插 ...

  8. Community Cloud零基础学习(一)启用以及简单配置

    本篇参考: https://trailhead.salesforce.com/en/content/learn/trails/communities https://trailhead.salesfo ...

  9. salesforce 零基础学习(四十七) 数据加密简单介绍

    对于一个项目来说,除了稳定性以及健壮性以外,还需要有较好的安全性,此篇博客简单描述salesforce中关于安全性的一点小知识,特别感谢公司中的nate大神和鹏哥让我学到了新得知识. 项目简单背景: ...

随机推荐

  1. ionic的start-y属性初始化页面

    混合式开发移动app,要实现的是初始化页面时不展示搜索框,让页面向上移动一定位移. <ion-content class="has-subheader" start-y=&q ...

  2. 近期编程问题——epoll failed:bad file descriptor

    出现问题:epoll_wait:Bad file descriptor 原因:IO时间的socket描述符在epoll_ctl处理前就关闭了. 解决方法:不要在epoll_ctl之前关闭socket描 ...

  3. windows系统c盘占满/linux系统磁盘block、inode占满处理

    windows系统 下载c盘清理.bat到服务器,双击bat文件将自动清理 linux系统 先远程ssh登录上服务器,登录教程:http://www.west263.com/faq/list.asp? ...

  4. 移动混合开发之android文件管理-->flexbox,webFont。

    增加操作栏,使用felxbox居中,felx相关参考网址:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 使用webFont添加图标, ...

  5. php 目录函数和日期函数

    continue . break . exit目录函数opendir(); 打开一个文件夹is_file 只判断文件是否存在: file_exists 判断文件是否存在或者是目录是否存在: is_di ...

  6. 安全协议系列(四)----SSL与TLS

    当今社会,电子商务大行其道,作为网络安全 infrastructure 之一的 -- SSL/TLS 协议的重要性已不用多说.OpenSSL 则是基于该协议的目前应用最广泛的开源实现,其影响之大,以至 ...

  7. 快速了解IOC的几种姿势

    一.首先我们了解IOC如何注入的几种姿势 构造函数注入(Constructor Injection) Ioc容器会智能的选择和调用合适的构造函数以创建依赖的对象.如果被选择的构造函数具有相应的参数,I ...

  8. docker push 实现过程

    这一篇文章分析一下docker push的过程:docker push是将本地的镜像上传到registry service的过程: 根据前几篇文章,可以知道客户端的命令是在api/client/pus ...

  9. mysql启动,关闭,重启

    1)启动: ? 1 sudo /etc/init.d/mysql start 2)停止: ? 1 sudo /etc/init.d/mysql stop 3)重启: ? 1 sudo /etc/ini ...

  10. Java基础一

    这是在网上找的知识点 覆盖方法必须满足的条件: 1)子类方法的名称.参数签名和返回类型必须与父类方法的名称.参数签名和返回类型一致,修饰符可以相同也可以不同,但子类的访问权限不能低于父类的访问权限. ...