您的位置:首页 > 其它

写一个MyList<T>的类,内部用T[]实现(不能使用系统的List<T>类),需要实现的接口如下:

2012-01-19 17:12 1106 查看
public interface IMyList<T> : IEnumerable , IEnumerable<T>

{

int Count { get; }

T this[int index] { get; set; }

int IndexOf(T item);

void Add(T item);

void Insert(int index, T item);

void RemoveAt(int index);

bool Remove(T item);

void Clear();

}

以上T可为值类型或者引用类型.

接口IMyList:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MyListPractice
{
//========================================================================
//  インターフェイス名 : IMyList
/// <summary>
/// IMyList接口
/// </summary>
/// <remarks>
/// int Count { get; }
/// T this[int index] { get; set; }
/// int IndexOf(T item);
/// void Add(T item);
/// void Insert(int index, T item);
/// void RemoveAt(int index);
/// bool Remove(T item);
/// void Clear();
/// </remarks>
//========================================================================
public interface IMyList<T> : IEnumerable<T>, IEnumerable
{
/// <summary>Countを取得する</summary>
/// <value></value>
int Count { get; }

/// <summary>Indexを設定/取得する</summary>
/// <value></value>
T this[int index]
{
get;
set;
}

//========================================================================
//  メソッド名 : IndexOf
/// <summary>
/// IndexOf
/// </summary>
/// <remarks>
/// 查找指定值的索引并返回其索引值
/// </remarks>
/// <param name="item"></param>
/// <returns>IndexOf</returns>
//========================================================================
int IndexOf(T item);

//========================================================================
//  メソッド名 : Add
/// <summary>
/// Add
/// </summary>
/// <remarks>
/// 添加指定的值
/// </remarks>
/// <param name="item">item</param>
//========================================================================
void Add(T item);

//========================================================================
//  メソッド名 : Insert
/// <summary>
/// Insert
/// </summary>
/// <remarks>
/// 在指定的位置插入指定的参数
/// </remarks>
/// <param name="index">index</param>
/// <param name="item">item</param>
//========================================================================
void Insert(int index, T item);

//========================================================================
//  メソッド名 : RemoveAt
/// <summary>
/// RemoveAt
/// </summary>
/// <remarks>
/// 从指定的位置移除
/// </remarks>
/// <param name="index">index</param>
//========================================================================
void RemoveAt(int index);

//========================================================================
//  メソッド名 : Remove
/// <summary>
/// Remove
/// </summary>
/// <remarks>
/// 移除指定的值,并搜索起索引并移除
/// </remarks>
/// <param name="item"></param>
/// <returns></returns>
//========================================================================
bool Remove(T item);

//========================================================================
//  メソッド名 : Clear
/// <summary>
/// Clear
/// </summary>
/// <remarks>
/// 清空所有的
/// </remarks>
//========================================================================
void Clear();
}
}


实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyListPractice
{
//========================================================================
//  クラス名 : MyClass
/// <summary>
/// 实现IMyClass接口
/// </summary>
//========================================================================
class MyClass<T> : IMyList<T>
{
private T[] data;
private int count = 0;

/// <summary>Capacityを設定/取得する</summary>
/// <value></value>
public int Capacity
{
get { return data.Length; }
set
{
if (value < count)
{
throw new ArgumentException("Illegal!");
}
else
{
if (value != data.Length)
{
T[] data1 = new T[value];
for (int i = 0; i < count; i++)
{
data1[i] = data[i];
}
data = data1;
}
}
}
}

//========================================================================
//  コンストラクタ名 : MyClass
/// <summary>
/// MyClass构造函数
/// </summary>
/// <remarks>
/// 给data赋初值
/// </remarks>
//========================================================================
public MyClass()
{
this.data = new T[4];
}

//========================================================================
//  コンストラクタ名 : MyClass
/// <summary>
/// MyClass有参数的构造函数
/// </summary>
/// <remarks>
/// 当参数小于零时,抛出非法参数异常,
/// 否则将数据传给data
/// </remarks>
/// <param name="capacity">capacity</param>
//========================================================================
public MyClass(int capacity)
{
if (capacity <= 0)
{
throw new ArgumentException("Illegal init" + capacity);
}

this.data = new T[capacity];
}

/// <summary>Countを取得する</summary>
/// <value>Count</value>
public int Count
{
get { return this.count; }
}

//========================================================================
//  メソッド名 : RemoveAt
/// <summary>
/// RemoveAt
/// </summary>
/// <remarks>
/// 当索引大于count数时,则抛出超出索引异常,
/// 否则根据Index将对应的值移除。
/// </remarks>
/// <param name="index"></param>
//========================================================================
public void RemoveAt(int index)
{
if (index >= count)
{
throw new ArgumentException("超出索引范围。");
}
else
{
for (int i = index; i < count; i++)
{
data[i] = data[i + 1];
}
count--;
}

}

//========================================================================
//  メソッド名 : Remove
/// <summary>
/// Remove
/// </summary>
/// <remarks>
/// 当在List中找到匹配的值时,将该值的索引移除,
/// 并返回true
/// </remarks>
/// <param name="item"></param>
/// <returns></returns>
//========================================================================
public bool Remove(T item)
{
for (int index = 0; index < count; index++)
{
if (object.Equals(data[index], item) == true)
{
RemoveAt(index);
return true;
}
}
return false;
}

/// <summary>indexを設定/取得する</summary>
/// <value></value>
public T this[int index]
{
get
{
if (index >= count)
{
throw new ArgumentException("超出索引范围。");
}
else
{
return data[index];
}
}
set
{
if (index >= count)
{
throw new ArgumentException("超出索引范围。");
}
else
{
data[index] = value;
}
}
}

//========================================================================
//  メソッド名 : IndexOf
/// <summary>
/// IndexOf
/// </summary>
/// <remarks>
/// 根据item查找出该值在List中的索引位置
/// </remarks>
/// <param name="item">item</param>
/// <returns>index</returns>
//========================================================================
public int IndexOf(T item)
{
int index = -1;
for (int i = 0; i < count; i++)
{
if (data[i].Equals(item))
{
index = i;
break;
}
}
return index;
}

//========================================================================
//  メソッド名 : Add
/// <summary>
/// Add
/// </summary>
/// <remarks>
/// 如果count值大于data的长度,则将长度*2,
/// 然后插入其值
/// </remarks>
/// <param name="item"></param>
//========================================================================
public void Add(T item)
{
if (count >= data.Length)
{
Capacity = Capacity * 2;
}
data[count] = item;
count++;
}

//========================================================================
//  メソッド名 : Insert
/// <summary>
/// Insert
/// </summary>
/// <remarks>
/// 当Index大于count数时,则抛出超出索引异常,
/// 如果count值大于data的长度,则将长度*2,
/// 然后再进行数据的插入
/// </remarks>
/// <param name="index">index</param>
/// <param name="item">item</param>
//========================================================================
public void Insert(int index, T item)
{
if (index >= count)
{
throw new ArgumentException("超出索引范围。");
}

if (count >= data.Length)
{
Capacity = Capacity * 2;
}

for (int i = count; i > index; i--)
{
data[i] = data[i - 1];
}
data[count] = item;
count++;
}

//========================================================================
//  メソッド名 : Clear
/// <summary>
/// Clear
/// </summary>
/// <remarks>
/// 清空数据
/// </remarks>
//========================================================================
public void Clear()
{
this.data = new T[4];
count = 0;
}

//========================================================================
//  メソッド名 : GetEnumerator
/// <summary>
/// 实现IEnumerable接口
/// </summary>
/// <remarks>
/// 实现IEnumerable接口里的GetEnumerator方法
/// </remarks>
/// <returns> data[i]</returns>
//========================================================================
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < data.Length; i++)
{
yield return data[i];
}
}

//========================================================================
//  メソッド名 : Collections.IEnumerable.GetEnumerator
/// <summary>
/// 实现Collections.IEnumerable接口
/// </summary>
/// <remarks>
/// 实现System.Collections.IEnumerable.GetEnumerator()方法
/// </remarks>
/// <returns>i</returns>
//========================================================================
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (T i in data)
{
yield return i;
}
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyListPractice
{
//========================================================================
//  クラス名 : MyListPractice
/// <summary>
/// MyListPractice
/// </summary>
/// <remarks>
/// 测试IMyList接口和MyClass类
/// </remarks>
//========================================================================
class MyListPractice
{
//========================================================================
//  メソッド名 : Main
//========================================================================
static void Main(string[] args)
{
MyClass<string> myClass = new MyClass<string>();

myClass.Add("Monday");
myClass.Add("Tuesday");
myClass.Add("Wednesday");
myClass.Add("Thursday");
myClass.Add("Friday");
myClass.Add("Saturday");
myClass.Add("Sunday");

Console.WriteLine("Count:{0}", myClass.Count);

Console.WriteLine("Init:{0}",myClass.Capacity);

Console.WriteLine("IndexOf(Wednesday):{0}", myClass.IndexOf("Wednesday"));

myClass.RemoveAt(5);
Console.WriteLine("RemoveAt(5) of Count:{0}", myClass.Count);

myClass.Remove("Sunday");
Console.WriteLine("Remove(Sunday) of Count:{0}", myClass.Count);

myClass.Insert(4, "Sunday");
Console.WriteLine("Insert of Count:{0}", myClass.Count);

foreach(string s in myClass)
{
Console.WriteLine(s);
}

Console.ReadKey();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐