Using HashTable to realize a set which Provide a collection that contains a group of unique objects

本文介绍了一个简单的Set类实现,该类使用Hashtable来确保元素的唯一性,并提供了添加元素、检查元素存在性和获取元素枚举器的方法。
ContractedBlock.gifExpandedBlockStart.gifSet
 1None.gifusing System;
 2None.gifusing System.Collections;
 3None.gifnamespace Utilities
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Provide a collection that contains a group of unique
 7InBlock.gif    /// objects.
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    public class Set
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        private Hashtable h = new Hashtable();
12ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
13InBlock.gif        /// Return an enumerator for this set.
14InBlock.gif        /// </summary>
15ExpandedSubBlockEnd.gif        /// <returns>An enumerator for this set</returns>

16InBlock.gif        public IEnumerator GetEnumerator()
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            return h.Keys.GetEnumerator();
19ExpandedSubBlockEnd.gif        }

20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
21InBlock.gif        /// Add the provided object to this set.
22InBlock.gif        /// </summary>
23ExpandedSubBlockEnd.gif        /// <param name="o">the object to add</param>

24InBlock.gif        public void Add(Object o)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            h[o] = null;
27ExpandedSubBlockEnd.gif        }

28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
29InBlock.gif        /// Return true if the set contains the presented object.
30InBlock.gif        /// </summary>
31InBlock.gif        /// <param name="o">the object of curiosity</param>
32ExpandedSubBlockEnd.gif        /// <returns>true, if the set contains the presented object</returns>

33InBlock.gif        public bool Contains(Object o)
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            return h.Contains(o);
36ExpandedSubBlockEnd.gif        }

37ExpandedSubBlockEnd.gif    }

38ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值