.NET学习者的有趣任务:为什么ArrayList.Sort会挂起? [.NET 1.1] [英] interesting task for .NET learners: why ArrayList.Sort hangs? [.NET 1.1]

查看:50
本文介绍了.NET学习者的有趣任务:为什么ArrayList.Sort会挂起? [.NET 1.1]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码应该对字符串的ArrayList进行排序,同时保持

一个固定的字符串。最后一个位置的元素。

但是,代码在.NET 1.1中挂起。我认为回答这些问题

对于.NET学习者来说应该是一个有趣且有启发性的任务:


1.为什么代码在.NET 1.1中挂起?

2.为什么它不挂在.NET 2.0中?

3.为什么当你保持固定时它不会挂起在.NET 1.1中元素在

第一个(而不是最后一个)位置:


if(ys ==" fixed")

返回1;

if(xs ==" fixed")

返回-1;


4.如何修复比较器的代码,以便在.NET 1.1中进行排序?


问候,

Wiktor Zychla


使用系统;

使用System.Collections;


类TestComparer:IComparer

{

public int比较(对象x,对象y)

{

if(!(x is string))return 0;

if(!(y)是字符串))返回0;


字符串xs = x为字符串;

字符串ys = y为字符串;


if(ys ==" fixed")

返回-1;

if(xs ==" fixed")

返回1;


返回xs.CompareTo(ys);

}

}


class Test

{

public static void Main()

{

ArrayList test = new ArrayList();


test.Add(" test2" );

test.Add(" test1");

test.Add(" fixed");

test.Add( " test5");

test.Add(" test4");

test.Add(" test3");


foreach(测试中的字符串)

Console.WriteLine(s);


Console.WriteLine();

test.Sort(new TestComparer());


foreach(测试中的字符串s)

Console.WriteLine(s);

}

}

Following code was supposed to sort an ArrayList of strings while keeping
one "fixed" element at the last position.
However, the code hangs in .NET 1.1. I think that answering these questions
should be an interesting and instructive task for .NET learners:

1. why the code hangs in .NET 1.1?
2. why it does not hang in .NET 2.0?
3. why it does not hang in .NET 1.1 when you keep the "fixed" element at the
first (instead of last) position:

if ( ys == "fixed" )
return 1;
if ( xs == "fixed" )
return -1;

4. how to fix the code of the comparer so that sorting works in .NET 1.1?

Regards,
Wiktor Zychla

using System;
using System.Collections;

class TestComparer : IComparer
{
public int Compare(object x, object y)
{
if (!( x is string )) return 0;
if (!( y is string )) return 0;

string xs = x as string;
string ys = y as string;

if ( ys == "fixed" )
return -1;
if ( xs == "fixed" )
return 1;

return xs.CompareTo( ys );
}
}

class Test
{
public static void Main()
{
ArrayList test = new ArrayList();

test.Add( "test2" );
test.Add( "test1" );
test.Add( "fixed" );
test.Add( "test5" );
test.Add( "test4" );
test.Add( "test3" );

foreach ( string s in test )
Console.WriteLine( s );

Console.WriteLine();
test.Sort( new TestComparer() );

foreach ( string s in test )
Console.WriteLine( s );
}
}

推荐答案

Wiktor Zychla [C#MVP]写道:
Wiktor Zychla [C# MVP] wrote:
以下代码应该对字符串的ArrayList进行排序,同时保持一个固定的字符串。最后一个位置的元素。
然而,代码在.NET 1.1中挂起。我认为回答这些问题对于.NET学习者来说应该是一个有趣且有启发性的任务:
Following code was supposed to sort an ArrayList of strings while keeping
one "fixed" element at the last position.
However, the code hangs in .NET 1.1. I think that answering these questions
should be an interesting and instructive task for .NET learners:




这是否意味着你宁愿那些人'新手不要

回答?


Jon



Does that mean you''re rather that people who aren''t newbies don''t
answer?

Jon


>这是否意味着你宁愿那些不是新手的人不要
> Does that mean you''re rather that people who aren''t newbies don''t
回答?




这取决于是否答案对你来说很明显。如果是 - 让

其他人自己想想并找到答案。


没有正确答案的奖品;)

Wiktor



it depends on whether the answer is obvious to you or not. if it is - let
others think and find the answer by themselves.

there are no prizes for correct answers ;)

Wiktor


Wiktor Zychla [C#MVP]写道:
Wiktor Zychla [C# MVP] wrote:
这意味着你宁愿那些不是新手的人不回答?
Does that mean you''re rather that people who aren''t newbies don''t
answer?



这取决于答案是否对你来说是显而易见的。如果是 - 让其他人自己思考并找到答案。

没有正确答案的奖品;)



it depends on whether the answer is obvious to you or not. if it is - let
others think and find the answer by themselves.

there are no prizes for correct answers ;)




没问题。我不确定你是否真的对

答案感兴趣,或者你是否*只是把它作为一个训练问题。


Jon



No problem. I wasn''t sure whether you were actually interested in the
answer or whether you were *just* posing it as a training question.

Jon


这篇关于.NET学习者的有趣任务:为什么ArrayList.Sort会挂起? [.NET 1.1]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆