以前从来没有用过泛型,正在msdn上看泛型。看到上面的个例子。
public class GenericList<T> //: System.Collections.Generic.IEnumerable<T> 原来的例子上此处没屏蔽
{
// The nested class is also generic on T
protected class Node
{
// T used in non-generic constructor
public Node(T t)
{
next = null;
data = t;
} private Node next;
public Node Next
{
get { return next; }
set { next = value; }
} // T as private member data type
private T data; // T as return type of property
public T Data
{
get { return data; }
set { data = value; }
}
} protected Node head; // constructor
public GenericList()
{
head = null;
} // T as method parameter type:
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
//屏掉,迭代器foreach无效,并且必须得为公共的
public IEnumerator<T> GetEnumerator()
{
Node current = head; while (current != null)
{
yield return current.Data;
current = current.Next;
}
} //System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
//{
// return GetEnumerator();
//}原来的例子上此处没屏蔽,为实现非泛型集合的迭代接口,个人觉得奇怪,此处定义的是泛型集口,可能出现非泛型的情况么? 请帮忙举个例子。
} public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{
// A simple, unoptimized sort algorithm that
// orders list elements from lowest to highest: public void BubbleSort()
{
if (null == head || null == head.Next)
{
return;
}
bool swapped; do
{
Node previous = null;
Node current = head;
swapped = false; while (current.Next != null)
{
// Because we need to call this method, the SortedList
// class is constrained on IEnumerable<T>
if (current.Data.CompareTo(current.Next.Data) > 0)
{
Node tmp = current.Next;
current.Next = current.Next.Next;
tmp.Next = current; if (previous == null)
{
head = tmp;
}
else
{
previous.Next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.Next;
}
}
} while (swapped);
}
} public class Person : System.IComparable<Person>
{
string name;
int age; public Person(string s, int i)
{
name = s;
age = i;
} // This will cause list elements to be sorted on age values.
public int CompareTo(Person p)
{
return age - p.age;
} public override string ToString()
{
return name + ":" + age;
} // Must implement Equals.
public bool Equals(Person p)
{
return (this.age == p.age);
}
} static void Main(string[] args)
{
SortedList<Person> list = new SortedList<Person>(); //Create name and age values to initialize Person objects.
string[] names = new string[]
{
"Franscoise",
"Bill",
"Li",
"Sandra",
"Gunnar",
"Alok",
"Hiroyuki",
"Maria",
"Alessandro",
"Raul"
}; int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 }; //Populate the list.
for (int x = 0; x < 10; x++)
{
list.AddHead(new Person(names[x], ages[x]));
} //Print out unsorted list.
foreach (Person p in list)
{
System.Console.WriteLine(p.ToString());
}
System.Console.WriteLine("Done with unsorted list"); //Sort the list.
list.BubbleSort(); //Print out sorted list.
foreach (Person p in list)
{
System.Console.WriteLine(p.ToString());
}
System.Console.WriteLine("Done with sorted list"); Console.ReadLine(); }
public class GenericList<T> //: System.Collections.Generic.IEnumerable<T> 原来的例子上此处没屏蔽
{
// The nested class is also generic on T
protected class Node
{
// T used in non-generic constructor
public Node(T t)
{
next = null;
data = t;
} private Node next;
public Node Next
{
get { return next; }
set { next = value; }
} // T as private member data type
private T data; // T as return type of property
public T Data
{
get { return data; }
set { data = value; }
}
} protected Node head; // constructor
public GenericList()
{
head = null;
} // T as method parameter type:
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
//屏掉,迭代器foreach无效,并且必须得为公共的
public IEnumerator<T> GetEnumerator()
{
Node current = head; while (current != null)
{
yield return current.Data;
current = current.Next;
}
} //System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
//{
// return GetEnumerator();
//}原来的例子上此处没屏蔽,为实现非泛型集合的迭代接口,个人觉得奇怪,此处定义的是泛型集口,可能出现非泛型的情况么? 请帮忙举个例子。
} public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{
// A simple, unoptimized sort algorithm that
// orders list elements from lowest to highest: public void BubbleSort()
{
if (null == head || null == head.Next)
{
return;
}
bool swapped; do
{
Node previous = null;
Node current = head;
swapped = false; while (current.Next != null)
{
// Because we need to call this method, the SortedList
// class is constrained on IEnumerable<T>
if (current.Data.CompareTo(current.Next.Data) > 0)
{
Node tmp = current.Next;
current.Next = current.Next.Next;
tmp.Next = current; if (previous == null)
{
head = tmp;
}
else
{
previous.Next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.Next;
}
}
} while (swapped);
}
} public class Person : System.IComparable<Person>
{
string name;
int age; public Person(string s, int i)
{
name = s;
age = i;
} // This will cause list elements to be sorted on age values.
public int CompareTo(Person p)
{
return age - p.age;
} public override string ToString()
{
return name + ":" + age;
} // Must implement Equals.
public bool Equals(Person p)
{
return (this.age == p.age);
}
} static void Main(string[] args)
{
SortedList<Person> list = new SortedList<Person>(); //Create name and age values to initialize Person objects.
string[] names = new string[]
{
"Franscoise",
"Bill",
"Li",
"Sandra",
"Gunnar",
"Alok",
"Hiroyuki",
"Maria",
"Alessandro",
"Raul"
}; int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 }; //Populate the list.
for (int x = 0; x < 10; x++)
{
list.AddHead(new Person(names[x], ages[x]));
} //Print out unsorted list.
foreach (Person p in list)
{
System.Console.WriteLine(p.ToString());
}
System.Console.WriteLine("Done with unsorted list"); //Sort the list.
list.BubbleSort(); //Print out sorted list.
foreach (Person p in list)
{
System.Console.WriteLine(p.ToString());
}
System.Console.WriteLine("Done with sorted list"); Console.ReadLine(); }
解决方案 »
- 我在主窗体的 formclosing事件中询问用户是否退出,为什么这个询问会出现两次呢?
- 急,win2003能安装SDK7.0吗?
- 如何控制软件的使用天数?
- int? success = -1;与int success = -1;区别? "?"有什么作用
- 请问winform中将文件复制到服务器指定文件夹该怎么做?
- 高手们帮帮小弟好吗,我把窗体的FormBorderstyle设为None.如何实现拉伸功能,还可以自由的拉大拦小
- 表单字段在服务器端判断和确认时,如何判断一个字段是否是int型的?
- 关于C#特性
- 怎样用C#编程做断点续传呢?
- 窗体起始位置的问题。
- 如何实现支付宝?
- 如何不引用Excel.dll把DatagridView导出成为Excel文件??
public IEnumerator<T> GetEnumerator()
{
Node current = head; while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
此方法就可以了。
System.Collections.Generic.IEnumerable<T> 这个接口完全没意义。
这里就有个现成的例子,是没有继承System.Collections.Generic.IEnumerable<T>接口而可以使用迭代的。
代码如下:
// type parameter T in angle brackets
public class GenericList<T>
{
// The nested class is also generic on T
private class Node
{
// T used in non-generic constructor
public Node(T t)
{
next = null;
data = t;
} private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
// T as private member data type
private T data; // T as return type of property
public T Data
{
get { return data; }
set { data = value; }
}
} private Node head;
// constructor
public GenericList()
{
head = null;
} // T as method parameter type:
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
} public IEnumerator<T> GetEnumerator()
{
Node current = head; while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
class TestGenericList
{
static void Main()
{
// int is the type argument
GenericList<int> list = new GenericList<int>(); for (int x = 0; x < 10; x++)
{
list.AddHead(x);
} foreach (int i in list)
{
System.Console.Write(i + " ");
}
System.Console.WriteLine("\nDone");
}
}
//{
// return GetEnumerator();
//}这个是为了能foreach
public void AddHead(object o)
{
Node n = new Node(o);
n.Next = head;
head = n;
}
如果对集合进行了更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,并且下一次对 MoveNext 或 Reset 的调用将引发 InvalidOperationException。