关于ArrayList以及简单哈希表中的一些常用的东东

来源:互联网 发布:2015年广告投放数据 编辑:程序博客网 时间:2024/05/17 02:15
(1),ArrayList  
     /**             * ArrayList 的对象,在不指明容量大小的情况下默认的是16,,而且在操作的过程中,当列表中的元素达到最大容量时,列表将自动将其容量增加一倍;             * 命名空间: using System.Collections;             * add 一次只能添加一个数据,addRange 一次可以添加多个数据;             * **/

  /**             * 在ArrayList中,支持删除操作的有三个方法             * Remove(object obj); 用于输出数组中特定的对象;             * RemoveAt(int index) ;  用于移除指定索引处的元素 。参数 index 为要移除的元素从 0 开始的索引;             * RemoveRange(int index,int count) ; 用于移除一定范围的元素; 参数 index 为要移除元素的起始索引(从0开始),count 表示要移除的元素的个数;             * **/            /**             * ArrayList 提供了二分查找的方法 BinarySearch(),他的返回值哦该元素 从0 开始的索引;             * **/

(2)哈希表(Hashtable)

       表的创建: 

   

class MyHashtable    {        public Hashtable htResult = new Hashtable(); // 构造一个哈希表;        public void AddItem(int _intNewItem)        {            int pkey = _intNewItem % 13;            int i = 0;            while (this.htResult.Contains(pkey) && i < 13)  //检查表中的该位置是不是已经有数据了,如果有,则检索下一个,依次不断的循环下去,直到找到合适的位置为止;            {                pkey = (pkey + 1) % 13;                i++;            }            if (i < 13)            {                this.htResult.Add(pkey, _intNewItem);            }            else            {                Console.WriteLine("哈希表溢出~~~");            }        }    }

(2) 表中元素的加入及输出操作:

      MyHashtable myht = new MyHashtable();            int pValue = 0;            Console.WriteLine("输入10个整数值:");            for (int i = 0; i < 10; i++)            {                pValue = Convert.ToInt32(Console.ReadLine());                myht.AddItem(pValue); // 将输入的整数压到哈希表中;            }            foreach (System.Collections.DictionaryEntry pair in myht.htResult)            {                Console.WriteLine("{0}->{1}",pair.Key,pair.Value);            }


原创粉丝点击