【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大神和鹏哥让我学到了新得知识. 项目简单背景: ...
随机推荐
- java多线程系列之 synchronized
一.synchronized基本原理 java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁.线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁. ...
- 移除project,testsuite,testcase级别所有的custom properties
// Remove all custom properties on Project level. If removed, custom properties cannnot be injected ...
- 循序渐进redis(一) —— redis的安装及可视化工具的使用
1.安装 注意事项: 1.安装gcc 2.编译带参数: make MALLOC=libc 2.可视化客户端工具 推荐使用RedisClient,提供了基本的CRUD以及过期设置等操作的图形化接口,在项 ...
- 实验二 简易版C语言文法
<程序>::=begin<语句串>end <语句串>::=<语句>{;<语句>} <语句>::=<赋值语句> < ...
- 自定义 Material Design风格的提示框
关闭 自定义 Material Design风格的提示框 2016-04-24 10:55 152人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 其实在14年谷歌 ...
- android原生ExpandableListView
android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差 ...
- c# System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本
前几天遇到了这个问题,情景是与oracle数据库连接的时候出现的.本机已经安装了客户端,使用toad数据库工具能够与数据库相连进行相关的操作.但是在使用代码进行连接的时候出现了这样的问题.找了好久,都 ...
- AJAX请求WebService
1.WebService代码 [WebMethod] [ScriptMethod(UseHttpGet = false)] public string GetObject() { User user ...
- oracle xmltype导入并解析Excel数据 (二)规则说明
规则表字段说明如下: 其中RULE_FUNC_CUSTOMIZE表示,用户自己写函数,去判断数据是否合法,存储的是函数的名字 此函数的参数只有一个,该列的值,字段类型是Varchar2, 校验失败的话 ...
- Android 终于官方支持按百分比来设置控件的宽高了
dependencies { compile 'com.android.support:percent:22.2.0' } 支持布局 PercentRelativeLayout <android ...