【jq】c#零基础学习之路(5)自己编写简单的Mylist<T>
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>的更多相关文章
- 【jq】c#零基础学习之路(1)Hello World!
从今天起我会持续发表,这个就是一个日记型的,学习编程是枯燥的,况且我们还是零基础. 学前准备 1.编译环境 vs2010.vs2012.vs2015...(本人用的是vs2010旗舰版).vs2010 ...
- 【jq】c#零基础学习之路(4)抽象类和密封
一.抽象类 1.抽象类不能被实例化 2.抽象类方法必需要实现 3.如何类中函数为抽象函数,其类也需要定义成抽象类 4.关键字 abstract ,函数重写 override. 二.密封类 1.密封类不 ...
- 【jq】c#零基础学习之路(3)继承和虚方法
c#只能继承一个基类和多个接口(0+) 父类:Human: class Human { public virtual Move() { Console.WriteLine("Human的虚方 ...
- 【jq】c#零基础学习之路(2)循环和分支
一.循环语句 1).do { //循环体,先运行一次. } while (true); 2). while (true) { //循环体 } 3). for (int i = 0; i < le ...
- python 零基础学习之路 02-python入门
不知不觉学习python已经两个月了,从一开始不知道如何对print的格式化,到现在可以手撸orm,这期间真的是 一个神奇的过程.为了巩固自己的基础知识,为后面的拓展埋下更好的伏笔,此文当以导师的博客 ...
- Android 零基础学习之路
第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正則表達式. 3.面向对象的抽象.封装,继承,多态.类与对象.对象初始化 ...
- salesforce零基础学习(七十九)简单排序浅谈 篇一
我们在程序中经常需要对数据列表进行排序,有时候使用SOQL的order by 不一定能完全符合需求,需要对数据进行排序,排序可以有多种方式,不同的方式针对不同的场景.篇一只是简单的描述一下选择排序,插 ...
- Community Cloud零基础学习(一)启用以及简单配置
本篇参考: https://trailhead.salesforce.com/en/content/learn/trails/communities https://trailhead.salesfo ...
- salesforce 零基础学习(四十七) 数据加密简单介绍
对于一个项目来说,除了稳定性以及健壮性以外,还需要有较好的安全性,此篇博客简单描述salesforce中关于安全性的一点小知识,特别感谢公司中的nate大神和鹏哥让我学到了新得知识. 项目简单背景: ...
随机推荐
- UDP丢包严重
项目要求udp能够达到10万的并发量,搞了几天,丢包严重, 今天终于解决了,原来是socket缓冲区设置的不够大已经jvm内存不够大
- JS设置弹出小窗口。
经常上网的朋友可能会到过这样一些网站,一进入首页立刻会弹出一个窗口,或者按一个连接或按钮弹出,通常在这个窗口里会显示一些注意事项.版权信息.警告.欢迎光顾之类的话或者作者想要特别提示的信息.其实制作这 ...
- 获得APP当前显示的viewController
UIViewController* topViewController = ((UINavigationController*)self.window.rootViewController).topV ...
- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] --11061188刘强
结对编程总结 队员:刘强(11061188) 林谋武(11061169) 结对编程: 结对编程的优点: 1. 两个人合作,相比于一个人自己奋斗而言,更能激发自己的潜能:我们在合作过程中,互相学习,互 ...
- mysql 查看数据库、表的基本命令
1:show databases; 查看所有的数据库,等同于select schema_name from information_schema.schemata\G.\G 替换;,以纵向报表的形式输 ...
- C#给DataTable添加序号、C#给DataTable添加合计、小计
/// <summary> /// 给DataTable添加序号 /// </summary> /// <param name= ...
- WordPress博客平台的搭建--基于Ubuntu14服务器
环境:阿里云服务器,系统Ubuntu14.04, 阿里云域名 大致流程:LNMP+WordPress 主要参考博客:VPS+LNMP+WordPress搭建个人网站/博客 遇到的问题: 1.在登陆域名 ...
- HDU 4336 容斥原理 || 状压DP
状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...
- Win10/UWP开发—SystemNavigationManager
Win10系统为确保所有应用中的一致导航体验,提供后退导航功能.当你的应用在手机.平板电脑上或者在支持系统后退功能的电脑或笔记本电脑上运行时,系统会在"后退"按钮被按下时通知你的应 ...
- css3 视距-perspective
视距-用来设置用户与元素3d空间Z平面之间的距离. 实例1: HTML: <div class="perspective"> <h3&g ...