了解了icollection接口、迭代以及泛型集合,下面再详细了解一下ilist接口。
通过msdn可以看到ilist接口有两种:
元素为object类型的ilist接口,可以放不同类型的对象引用;
ilist8742468051c85b06f0a0af9e3e506b5c泛型接口,只能存放指定类型的对象引用。
其实,ilist和ilist8742468051c85b06f0a0af9e3e506b5c也称之为向量,特点是可以动态的改变集合的长度,无需确定集合的初始长度,集合会随着存放数据的数量自动变化。
可以看到ilist和ilist8742468051c85b06f0a0af9e3e506b5c的继承关系:
[comvisibleattribute(true)]
public interface ilist : icollection, ienumerable
public interface ilist<t> : icollection<t>,
ienumerable<t>, ienumerable
现在再返回去看下,ilist和ilist<t>的区别,看如下代码,arraylist是继承ilist的,list继承ilist<t>:
public class ilistclass
{
void test()
{
testclass1 c1 = null;
arraylist arrylist = new arraylist();
arrylist.add(c1);
list<testclass1> list = new list<testclass1>();
list.add(c1);
//取值
testclass1 getc1array = arrylist[0] as testclass1;//必须要一次强制转换
testclass1 getc1list = list[0];//不需要转换,所谓泛型
}
}
public class testclass1
{
}
这下就比较明白了。
一、ilist接口概述
ilis接口从icollection接口继承,具备以下特性,
count属性——获取集合元素个数;
getenumerator方法——可以迭代;
copyto方法——将指定元素复制到另一个数组中;
clear方法——清空整个集合。
ilist新增特性,
索引器属性——根据索引访问集合中任意元素;
add方法——向集合末尾添加元素;
insert方法——向集合指定位置插入元素;
remove方法——移除指定元素;(包括removeat)
contains方法——判断对象是否在集合中;
indexof方法——查找指定对象在集合中的索引位置。
另外,ilist接口集合按照顺序存放元素,不改变元素存放顺序。
2、算法
向量集合和数组一样,具备随即访问的特点。即无论访问向量集合的任何一个单元,所需的访问时间是完全相同的。在向量类中,实际上依然使用普通数组来记录集合数据,向量类使用了一些算法技巧,让整个类对外表现不同于普通数组的重要特点:可以动态改变数组长度。具体算法如下:
在内部数组足够长的情况下,直接进行添加和插入操作,在内部数组长度不足的情况下,按照内部数组长度增加2倍作为新的数组的长度,然后进行数据搬移(即把就数组数组移到新数组中)。向量在分配元素存储空间时,会多分配一些冗余空间,尽量减少内存分配次数。在数据删除时,并不改变内部数组长度,仅仅是使用被删除数据之后的数据覆盖被删除的数据。
不过向量每次分配空间时都多分配一些冗余空间,会造成内存的压力,因此在程序中应该尽量避免集中的次数繁多的内存分配。
三、实现类
ilist和ilist<t>的实现类,分别是arraylist类和list<t>类。
arraylist类处于system.collection命名空间下;
list<t>类处于system.collection.specialized命名空间下。
四、实现代码(非泛型)
/// <summary>
/// 实现ilist,非泛型
/// </summary>
public class arraylist : ilist
{
/// <summary>
/// 迭代
/// </summary>
public struct enumertor : ienumerator
{
/// <summary>
/// 迭代索引
/// </summary>
private int index;
/// <summary>
/// 迭代器所属的向量类对象的引用
/// </summary>
private arraylist arraylist;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="arraylist">迭代器所属的集合类</param>
public enumertor(arraylist arraylist)
{
this.arraylist = arraylist;
this.index = -1;
}
/// <summary>
/// 获取当前对象,根据index的值返回向量对应的对象引用
/// </summary>
public object current
{
get
{
return arraylist[index];
}
}
/// <summary>
/// 将迭代器指向下一个数据位置,通过改变index的值,加1或减1
/// </summary>
/// <returns></returns>
public bool movenext()
{
if (this.index < arraylist.count)
{
++this.index;
}
return this.index < arraylist.count;
}
/// <summary>
/// 迭代器回到起始位置,将index置为-1
/// </summary>
public void reset()
{
this.index = -1;
}
}
/// <summary>
/// 保存集合的数组
/// </summary>
private object[] array = new object[1];
/// <summary>
/// 当前集合的长度
/// </summary>
private int count;
/// <summary>
/// 默认构造函数
/// </summary>
public arraylist()
{
}
/// <summary>
/// 参数构造函数,通过参数指定内部数组长度,减少重新分配空间
/// </summary>
/// <param name="capacity"></param>
public arraylist(int capacity)
{
if (capacity < 0)
{
throw new exception();
}
if (capacity == 0)
{
capacity = 1;
}
this.array = new object[capacity];
this.count = 0;
}
public int count
{
get
{
return this.count;//该属性只读
}
}
/// <summary>
/// 集合实际使用长度
/// </summary>
public int capacity
{
get
{
return this.array.length;
}
}
/// <summary>
/// 是否固定大小
/// </summary>
public bool isfixedsize
{
get
{
return false;
}
}
/// <summary>
/// 是否只读集合
/// </summary>
public bool isreadonly
{
get
{
return false;
}
}
/// <summary>
/// 是否同步,即是否支持多线程访问
/// </summary>
public bool issynchronized
{
get
{
return false;
}
}
/// <summary>
/// 同步对象
/// </summary>
public object syncroot
{
get
{
return null;
}
}
/// <summary>
/// 当array长度不足时,重新分配新的长度足够的数组
/// </summary>
/// <returns></returns>
private object[] getnewarray()
{
return new object[(this.array.length + 1) * 2];
}
public int add(object value)
{
int newcount = this.count + 1;
if (this.array.length < newcount)//长度不足
{
object[] newarray = getnewarray();
array.copy(this.array, newarray, this.count);
this.array = newarray;//重新引用,指向新数组
}
//增加新元素
this.array[this.count] = value;
this.count = newcount;
//返回新元素的索引位置
return this.count - 1;
}
/// <summary>
/// 索引器属性,按索引返回向量中的某一项
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public object this[int index]
{
get
{
if (index < 0 || index >= this.count)
{
throw new exception();
}
return this.array[index];
}
set
{
if (index < 0 || index >= this.count)
{
throw new exception();
}
this.array[index] = value;
}
}
/// <summary>
/// 删除集合中的元素
/// </summary>
/// <param name="index"></param>
/// <param name="count"></param>
public void removerange(int index, int count)
{
if (index < 0)
{
throw new exception();
}
int removeindex = index + count;//计算集合中最后一个被删元素的索引
if (count < 0 || removeindex > this.count)
{
throw new exception();
}
//删除其实是将要删除元素之后的所有元素拷贝到要删除元素的位置覆盖掉
array.copy(this.array, index + 1, this.array, index + count - 1, this.count - removeindex);
//重新设置集合长度
this.count -= count;
}
/// <summary>
/// 查找对应的数组项,实际是遍历查找
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int indexof(object value)
{
int index = 0;
if (value == null)
{
while (index < this.count)
{
if (this.array[index] == null)
{
return index;
}
++index;
}
}
else
{
while (index < this.count)
{
if (this.array[index].equals(value))
{
return index;
}
++index;
}
}
return -1;
}
/// <summary>
/// 从集合中删除指定元素
/// </summary>
/// <param name="value"></param>
public void remove(object value)
{
int index = this.indexof(value);
if (index >= 0)
{
this.removerange(index, 1);
}
}
/// <summary>
/// 从集合中删除指定位置的元素
/// </summary>
/// <param name="index"></param>
public void removeat(int index)
{
removerange(index, 1);
}
/// <summary>
/// 获取最后一个元素的引用后删除最后一个元素
/// </summary>
/// <returns></returns>
public object popback()
{
object obj = this.array[this.count - 1];
removeat(this.count - 1);
return obj;
}
/// <summary>
/// 获取第一个元素引用并删除第一个元素
/// </summary>
/// <returns></returns>
public object propfront()
{
object obj = this.array[0];
removeat(0);
return obj;
}
/// <summary>
/// 插入元素
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
public void insert(int index, object value)
{
if (index >= this.count)
{
throw new exception();
}
//插入元素当空间不足时也是声明新的2倍长度数组,并拷贝旧数据。
//插入数据原理是,将指定位置后的数据全部后移,再将新数据放在指定位置。
int newcount = this.count + 1;
if (this.array.length < newcount)
{
object[] newarray = getnewarray();
array.copy(this.array, newarray, index);
this.array = newarray;
}
array.copy(this.array, index, this.array, index + 1, this.count - index);
this.array[index] = value;
this.count = newcount;
}
/// <summary>
/// 查看当前集合是否包含指定对象
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool contains(object value)
{
return this.indexof(value) >= 0;
}
/// <summary>
/// 将集合的长度改变为实际长度
/// </summary>
public void trimtosize()
{
//为了消除add和insert时增加的冗余,原理是新生成一个和实际长度相同的数组,然后将值全部移过来。
if (this.array.length > this.count)
{
object[] newarray = null;
if (this.count > 0)
{
newarray = new object[this.count];
array.copy(this.array, newarray, this.count);
}
else
{
newarray = new object[1];
}
this.array = newarray;
}
}
/// <summary>
/// 清空集合
/// </summary>
public void clear()
{
this.count = 0;
}
/// <summary>
/// 获取集合的迭代器
/// </summary>
/// <returns></returns>
public ienumerator getenumerator()
{
enumertor enumerator = new enumertor(this);
return enumerator;
}
/// <summary>
/// 转移集合元素
/// </summary>
/// <param name="targetarray"></param>
/// <param name="index"></param>
public void copyto(array targetarray, int index)
{
array.copy(this.array, 0, targetarray, index, this.count);
}
}
调用测试:
static void main(string[] args)
{
//调用测试
arraylist myarraylist = new arraylist();
myarraylist.add(40);
myarraylist.add(80);
myarraylist.add("hello");
//使用for循环遍历
for (int i = 0; i < myarraylist.count; i++)
{
console.writeline(myarraylist[i]);
}
console.writeline("---------------------");
//使用迭代循环
foreach (object obj in myarraylist)
{
console.writeline(obj);
}
console.writeline("---------------------");
myarraylist.insert(1, "insert");
foreach (object obj in myarraylist)
{
console.writeline(obj);
}
console.writeline("---------------------");
myarraylist.remove("insert");
foreach (object obj in myarraylist)
{
console.writeline(obj);
}
console.writeline("---------------------");
myarraylist.removeat(1);
foreach (object obj in myarraylist)
{
console.writeline(obj);
}
console.writeline("---------------------");
myarraylist.clear();
foreach (object obj in myarraylist)
{
console.writeline(obj);
}
console.writeline("---------------------");
random rand = new random();
for (int i = 0; i < 10; i++)
{
myarraylist.add(rand.next(10));
}
foreach (object obj in myarraylist)
{
console.writeline(obj);
}
console.writeline("---------------------");
console.writeline("集合是否包含为1的元素 ? " + (myarraylist.contains(0) ? "包含" : "不包含"));
console.writeline("元素1的位置 " + myarraylist.indexof(1));
console.readline();
}
结果:
以上就是的内容。