享受代码,享受人生

SOA is an integration solution. SOA is message oriented first.
The Key character of SOA is loosely coupled. SOA is enriched
by creating composite apps.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

HashTable Quiz

Posted on 2005-03-04 19:03  idior  阅读(1440)  评论(7编辑  收藏  举报
今天复习随机算法的时候,顺便想到的,大家做做看 (环境vs2003),如果需要我会提供答案。

using System;
using System.Collections;
namespace HashTable
{
    
    
public class MyKey
    
{
        
public int Key
        
{
            
set
            
{
                key 
= value;
            }

        }


        
private  int key=0;
        
public MyKey(int num)
        
{
            key
=num;
        }

        
public override int GetHashCode()
        
{
            
return key%5;
        }


        
public override bool Equals(object obj)
        
{
            
return key==((MyKey)obj).key;
                        
        }

    


    }


    
class HashTableTest
    
{
        
        [STAThread]
        
static void Main(string[] args)
        
{
            MyKey key1
=new MyKey(10);
            
            Hashtable ht
=new Hashtable();
            ht.Add(key1,
"Key1");

            key1.Key
=11;
            
bool isContained=ht.ContainsKey(key1);
            Console.Out.WriteLine(
"Is key1 contained in hashtable? {0}", isContained);

            MyKey key2
=new MyKey(10);
            ht.Add(key2,
"key2");
            Console.Out.WriteLine(
"Hashtable counts:"+ht.Count);

            MyKey key3
=new MyKey(15);
            ht.Add(key3,
"key3");
            Console.Out.WriteLine(
"Hashtable counts:"+ht.Count);

            MyKey key4
=new MyKey(10);
            ht.Add(key4,
"key4");



        }

    }

}