今天学习使用ArrayList.sort(compare)遇到了一些不解的问题,还请高手指点,小弟初学入门~~~别扔砖哦~~
首先请看CSDN的实例代码:
using System;
using System.Collections;public class SamplesArrayList
{ public class myReverserClass : IComparer
{ // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(y, x));
} } public static void Main()
{ // Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("The");
myAL.Add("quick");
myAL.Add("brown");
myAL.Add("fox");
myAL.Add("jumps");
myAL.Add("over");
myAL.Add("the");
myAL.Add("lazy");
myAL.Add("dog"); // Displays the values of the ArrayList.
Console.WriteLine("The ArrayList initially contains the following values:");
PrintIndexAndValues(myAL); // Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine("After sorting with the default comparer:");
PrintIndexAndValues(myAL); // Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort(myComparer);
Console.WriteLine("After sorting with the reverse case-insensitive comparer:");
PrintIndexAndValues(myAL);
Console.ReadKey(); } public static void PrintIndexAndValues(IEnumerable myList)
{
int i = 0;
foreach (Object obj in myList)
Console.WriteLine("\t[{0}]:\t{1}", i++, obj);
Console.WriteLine();
}}
现在想问实例中应用的比较器是如何工作的,如何输出反序的字符串呢?
我单步调试的时候,发现X,Y的取值很没有规律啊,不知道其中的算法是怎么设计的。
首先请看CSDN的实例代码:
using System;
using System.Collections;public class SamplesArrayList
{ public class myReverserClass : IComparer
{ // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(y, x));
} } public static void Main()
{ // Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("The");
myAL.Add("quick");
myAL.Add("brown");
myAL.Add("fox");
myAL.Add("jumps");
myAL.Add("over");
myAL.Add("the");
myAL.Add("lazy");
myAL.Add("dog"); // Displays the values of the ArrayList.
Console.WriteLine("The ArrayList initially contains the following values:");
PrintIndexAndValues(myAL); // Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine("After sorting with the default comparer:");
PrintIndexAndValues(myAL); // Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort(myComparer);
Console.WriteLine("After sorting with the reverse case-insensitive comparer:");
PrintIndexAndValues(myAL);
Console.ReadKey(); } public static void PrintIndexAndValues(IEnumerable myList)
{
int i = 0;
foreach (Object obj in myList)
Console.WriteLine("\t[{0}]:\t{1}", i++, obj);
Console.WriteLine();
}}
现在想问实例中应用的比较器是如何工作的,如何输出反序的字符串呢?
我单步调试的时候,发现X,Y的取值很没有规律啊,不知道其中的算法是怎么设计的。
解决方案 »
- c#做音乐播放器
- C# 怎么自定义Int32 的位表
- C#:窗体1,showDialog()时会卡住一会,然后才能正常工作,我是vista环境,主窗口是hide()的状态
- 一个关于异步HttpListener的问题
- 关于axwebbrowser newwindow2 e.ppDisp 怪问题
- 求助:如何更改HScrollBar的最大值的属性类型为double
- 关于TcpListner和TcpClient的问题
- 在数组byte[]如何表示回车?
- 【求助】WPF C#如何访问<Window.Resources>中DataTemplate里的控件
- C#里可以实现类似VC++的消息响应吗?
- VS2005(VC++)如何可以调用C#的WCF Service?
- 如何判断本地某程序是否安装了?????????
你写一个类,实现IComparer接口,提供Compare方法。
然后创建这个类的实例,调用某个集合对象的Sort方法,sort方法内部通过参数,获得你类的实例,调用Compare方法来比较两个元素根据返回值判断大小,这样对于一些没有直接关系的对象也就可以比较了。1.你编写一个实现IComparer接口的类->myReverserClass,提供Compare方法
2.调用Sort方法->Sort方法通过你传递的myReverserClass的对象,调用Compare方法来比较两个对象。这样的好处就是你只需要负责说明如何比较对象就可以了,至于如何排序,这是.net实现的。
但是我还是不太明白,您的意思是,我告诉它比较的原则,.net就可以默认的把ArrayList里面所有的元素自动比较,并排序吗?(没有什么规律吗?就像冒泡法一样是逐个比较,而用Compare单步调试时,发现X,Y的选值一定都没有规律。。) 还有一个问题,弱弱的问一下,设计模式,是不是每个软件人员必学的东西。
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(y, x));
}
意思是对myReverserClass : IComparer的实现是由
((new CaseInsensitiveComparer()).Compare(y, x))的返回值决定,照字面意思就是大小写不敏感的比较,由由于两个参数与传入的参数顺序是相反的所以就实现了反序。
CaseInsensitiveComparer 类 比较两个对象是否相等,比较时忽略字符串的大小写。命名空间: System.Collections
程序集: mscorlib(在 mscorlib.dll 中)