今天看一下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">}