Skip to content

Instantly share code, notes, and snippets.

@rosiu
Created March 28, 2012 05:06
Show Gist options
  • Save rosiu/2223799 to your computer and use it in GitHub Desktop.
Save rosiu/2223799 to your computer and use it in GitHub Desktop.
用数组去实现的,为了删除方便,定义了一个索引器.
using System;
using System.Collections;
using System.Collections.Generic;
namespace TESTESTE
{
public class MyList<T> : IMyList<T>
{
private T[] _data;
public MyList(T[] _arr)
{
this._data = _arr;
}
public MyList() {}
public T this[int i]
{
get
{
if (_data != null) {
if (i > 0 && i < _data.Length) {
return _data[i];
} else {
throw new IndexOutOfRangeException();
}
} else {
throw new NullReferenceException();
}
}
set
{
if (_data != null) {
if (i >= 0 && i <= _data.Length) {
_data[i] = value;
} else {
throw new IndexOutOfRangeException();
}
}
}
}
#region IMyList<T> 成员
public void AddFirst(T item)
{
if (_data == null || _data.Length == 0) {
_data = new T[1];
_data[0] = item;
} else {
T[] _result = new T[_data.Length + 1];
_result[0] = item;
for (int i = 0; i < _data.Length; i++) {
_result[i + 1] = _data[i];
}
_data = _result;
}
}
public void AddLast(T item)
{
if (_data == null || _data.Length == 0) {
_data = new T[1];
_data[0] = item;
} else {
T[] _result = new T[_data.Length + 1];
for (int i = 0; i < _data.Length; i++) {
_result[i] = _data[i];
}
_result[_data.Length] = item;
_data = _result;
}
}
public void Remove(T item)
{
if (_data != null && _data.Length > 0) {
int iPos = -1;
for (int i = 0; i < _data.Length; i++) {
if (_data[i].Equals(item)) {
iPos = i;
break;
}
}
if (iPos != -1) {
T[] _result = new T[_data.Length - 1];
for (int i = 0; i < iPos; i++) {
_result[i] = _data[i];
}
for (int i = iPos + 1; i < _data.Length; i++) {
_result[i - 1] = _data[i];
}
_data = _result;
}
}
}
public void Reserve()
{
if (_data != null && _data.Length > 0) {
T[] _result = new T[_data.Length];
int j = _data.Length-1;
for (int i = 0; i < _data.Length; i++) {
_result[i] = _data[j--];
}
_data = _result;
}
}
#endregion
#region IEnumerable<T> 成员
public IEnumerator<T> GetEnumerator()
{
return new MyListEnum<T>(this._data);
}
#endregion
#region IEnumerable 成员
IEnumerator IEnumerable.GetEnumerator()
{
return new MyListEnum<T>(this._data);
}
#endregion
}
public class MyListEnum<T> : IEnumerator<T>
{
public T[] _data;
private int _index=-1;
public MyListEnum(T[] _arr)
{
this._data = _arr;
}
#region IEnumerator<T> 成员
T IEnumerator<T>.Current
{
get
{
try {
return _data[_index];
} catch (IndexOutOfRangeException) {
throw new InvalidOperationException();
}
}
}
#endregion
#region IDisposable 成员
public void Dispose()
{
}
#endregion
#region IEnumerator 成员
object IEnumerator.Current
{
get
{
try {
return (object)_data[_index];
} catch (IndexOutOfRangeException) {
throw new InvalidOperationException();
}
}
}
public bool MoveNext()
{
_index++;
return _index < _data.Length;
}
public void Reset()
{
_index = -1;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TESTESTE
{
class Program
{
static void Main(string[] args)
{
MyList<MyClass> test = new MyList<MyClass>();
for (int i = 0; i < 10; i++) {
test.AddLast(new MyClass() {
ID = i,
Name = "Name" + i,
Sex = i % 2 == 0 ? "男" : "女"
});
}
test.AddFirst(new MyClass() {
ID = 100,
Name = "第一个",
Sex = "人妖"
});
test.Remove(test[5]);
test.Reserve();
foreach (MyClass item in test) {
Console.WriteLine("最终结果:编号—"+item.ID + ",姓名—" + item.Name + ",性别—" + item.Sex);
}
Console.WriteLine("=========以下为String类型测试============");
MyList<string> listStr = new MyList<string>();
for (int i = 0; i < 40; i++) {
listStr.AddLast("TEST-String-" + i.ToString());
}
listStr.AddFirst("添加到第一个,但reserve后会到最后");
listStr.Remove("TEST-String-10");
listStr.Remove("TEST-String-20");
listStr.Reserve();
foreach (string s in listStr) {
Console.WriteLine(s);
}
Console.ReadLine();
}
}
class MyClass
{
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
public string Sex
{
get;
set;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment