非泛型集合ArrayList和HashTable

    今天看一下ArrayList和HashTable。ArrayLis的使用非常简单,首先得手动Using一个库System.collection ,哈希表也同样得引入该库。

首先定义非泛型集合ArrayList,

ArrayList List = new ArrayList();
</pre>然后加入对象<pre name="code" class="csharp">list.add(stu1);
list.add(stu2);

删除则调用removed方法就可以了。ArrayList可以自动加入新对象而不用事先定义空间大小,其间的代码也应该非常简单,应该类似与c的malloc(sizeof(stu1));之类的。

然而访问ArrayList需要使用索引访问,如果数据过多,变化很大的话则非常难以跟踪对象的索引值,所以在ArrayList上添加了HashTable,使用函数传递的key来检索对应位置,然后可以方便的操作对应的对象。

     HashTable更像是给对象取了一个别名,通过这个别名来找到对象,而不用理会其索引。

HashTable list  = nwe lHashTable();
list.add("key1",stu1);
list.add("key2", stu2);
list["key1"];
list.remove("key1");<pre name="code" class="html">student obj = (student) list["key2"]

 哈希表的遍历不同于arrayList,哈希表遍历可以遍历其至或者其key 
foreach(<span style="color:#3333ff;">string</span> key in list.keys)
{
</pre><pre name="code" class="html">}
//or
foreach(<span style="color:#ff0000;">object</span> <span style="color:#ff0000;">item</span> in list.value)
{
</pre><pre name="code" class="html">}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值