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. Oracle 行转列(不固定行数的行转列,动态)(转)

    http://bbs.csdn.net/topics/330039676 SQLSERVER :行列转换例子:  http://www.cnblogs.com/gaizai/p/3753296.htm ...

  2. 对于.NET Socket连接的细节记录

    如果客户端直接连接一个不存在的服务器端,客户端会抛出异常: 如果在连接过程中,客户端强制关闭了连接(没有调用Close直接关闭了程序),服务器端会抛出异常: 如果在连接过程中,客户端调用了Close, ...

  3. Fragment中的onKeyDown事件让Activity处理--处理特殊按键比如移动终端扫描

    一些特殊按键事件需要在Activity中处理public void onKeyDown(int keyCode, KeyEvent event){ //让Activity处理 getActivity( ...

  4. Java InMemoryCache

    package pay.infrastructure.helper; import org.apache.commons.collections.MapIterator; import org.apa ...

  5. 《java小应用程序(Applet)和java应用程序(Application)分别编写的简单计算器》

    Application和Java Applet的区别.Java语言是一种半编译半解释的语言.Java的用户程序分为两类:Java Application和Java Applet.这两类程序在组成结构和 ...

  6. 调用javaAPI访问hive

    jdbc远程连接hiveserver2 2016-04-26 15:59 本站整理 浏览(425)     在之前的学习和实践Hive中,使用的都是CLI或者hive –e的方式,该方式仅允许使用Hi ...

  7. Vector和ArrayList的比较

    今天研究了一下Vector和ArrayList的源码,又加深了对这两个类的理解. List接口下一共实现了三个类:ArrayList,Vector,LinkedList.LinkedList就不多说了 ...

  8. 2016 - 3 - 12 SQLite的学习之SQL语言入门

    1.SQL语句的特点: 1.1 不区分大小写 1.2 每条语句以;结尾 2.SQL语句中常用关键字: select,insert,update,from,create,where,desc,order ...

  9. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  10. js 闭包演示

    function test2() { var scope = "global scope"; var f = enclose(scope); scope = 'aaa'; aler ...