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

(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);
            }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值