sky_handsome's profile天意PhotosBlogLists Tools Help

Blog


    April 17

    测试一下你是否是一个合格的程序员。

        class MyKey

        {

            public string s;

            public int data;

     

            public override bool Equals(object obj)

            {

                MyKey t = obj as MyKey;

                return t.s.Equals(s) && t.data == data;

            }

     

            public override int GetHashCode()

            {

                return data;

            }

        }

     

        class Program

        {

            public static void Main()

            {

                Dictionary<MyKey, object> d = new Dictionary<MyKey, object>();

                Random r = new Random((int)DateTime.Now.Ticks);

                for (int i = 0; i < 100000; ++i)

                {

                    MyKey k = new MyKey();

                    k.data = r.Next(Int32.MaxValue);

                    d.Add(k, null);

                }

            }

     

        }

     

    如果你一眼就看出来这个程序有什么问题而且预测到了正确行为结果,那你就非常合格了。可以去任何一家IT公司打工了。

     

    如果你没有预测到,请问下看:

    结果会是randomly的在运行到很多很多次循环以后,程序挂了,NullPointerException.

     

    如果你刚才预测错了请继续:

     

    如果你现在马上就知道为什么程序会挂而且解释清楚原因,你也基本合格了。

     

    如果你想了很久还是不知道为什么,那你基本不合格了。

     

    测试而已,娱乐娱乐。

     

     

    Comments (7)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    洋 施wrote:
    暑假时候写了一个程序。设置一个计时器,在响应代码中申请一个GDI对象,画图然后释放,发现运行半个小时左右就挂了。使用MFC做的。和这个应该是一个道理,但是我并不真正明白这是为什么。我想是C语言自身设计的问题,申请资源时候,地址无限增益,最终超出寻址范围。还请您给出最后的结论,谢谢
    Sept. 13
    In the first,using Dictionary to save Key and value for more times will have create same key and value ,so the problem is that when same key and value is in the dictionary .
    July 22
    you are right, sorry for forgetting that.
    Apr. 24
    Yes. You are almost right. So you are almost qualified. The equal function will trigered not really when the data conflicts but the hashvalue fall into the same slot of the Hashtable. This two conditions aren't equal unless your slot is large enough to handle all integer data which is 4G in memory.
    Apr. 24
    因为你的hashcode值是data,data是randomly produced, only when the data conflicts the equal() function will triggered to test whether hash conflicts, of course, sometime it won't fail for produced 100000 non-equal values.
    Apr. 24
    of course s is null, the problem is why it will fail, and why it will fail randomly but not at the first insertion
    Apr. 23
    s NULL?
    Apr. 22

    Trackbacks

    The trackback URL for this entry is:
    http://sky1983121.spaces.live.com/blog/cns!2E93CC4DC32FC730!936.trak
    Weblogs that reference this entry
    • None