[转]ArrayList和HashTable 使用

[转]ArrayList和HashTable 使用

System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。

一.优点

1。支持自动改变大小的功能

2。可以灵活的插入元素

3。可以灵活的删除元素

二.局限性

跟一般的数组比起来,速度上差些

三.添加元素

1.  public virtual int Add(object value);

将对象添加到 ArrayList 的结尾处

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

内容为a b c d e

2.  public virtual void Insert(int index,object value);

  将元素插入 ArrayList 的指定索引处

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

aList.Insert(
0,"aa");

结果为aa a b  c d e

3.  public virtual void InsertRange(int index,ICollection c);

   将集合中的某个元素插入 ArrayList 的指定索引处

  ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

ArrayList list2
= new ArrayList();

        list2.Add(
"tt");

list2.Add(
"ttt");

aList.InsertRange(
2,list2);

结果为a b tt ttt c d e

四.删除

a)      
public virtual void Remove(object obj);

   从 ArrayList 中移除特定对象的第一个匹配项,注意是第一个

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

aList.Remove(
"a");

结果为b c d e

2. public virtual void RemoveAt(int index);

移除 ArrayList 的指定索引处的元素

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

aList.RemoveAt(
0);

结果为b c d e

3.  public virtual void RemoveRange(int index,int count);

从 ArrayList 中移除一定范围的元素。

Index表示索引,count表示从索引处开始的数目

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

aList.RemoveRange(
1,3);

结果为a e

4.  public virtual void Clear();

从 ArrayList 中移除所有元素。

五.排序

a)      
public virtual void Sort();

对 ArrayList 或它的一部分中的元素进行排序。

ArrayList aList
= new ArrayList();

aList.Add(
"e");

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

DropDownList1.DataSource
= aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

结果为e a b c d

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

aList.Sort(); 
//排序

DropDownList1.DataSource
= aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

结果为a b c d e

b)      
public virtual void Reverse();

将 ArrayList 或它的一部分中元素的顺序反转。

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

aList.Reverse(); 
//反转

DropDownList1.DataSource
= aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

结果为 e d c b a

六.查找

a)      
public virtual int IndexOf(object);

b)      
public virtual int IndexOf(object, int);

c)      
public virtual int IndexOf(object, int, int);

  返回 ArrayList 或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回
-1

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");

int nIndex = aList.IndexOf(“a”);  //1

nIndex
= aList.IndexOf(“p”);     //没找到,-1

d)      
public virtual int LastIndexOf(object);

e)        
public virtual int LastIndexOf(object, int);

f)        
public virtual int LastIndexOf(object, int, int);

  返回 ArrayList 或它的一部分中某个值的最后一个匹配项的从零开始的索引。

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"a");  //同0

aList.Add(
"d");

aList.Add(
"e");

int nIndex = aList.LastIndexOf("a");  //值为2而不是0

g)      
public virtual bool Contains(object item);

确定某个元素是否在 ArrayList 中。包含返回true,否则返回false

七.其他

1public virtual int Capacity {get; set;}

获取或设置 ArrayList 可包含的元素数。

2public virtual int Count {get;}

获取 ArrayList 中实际包含的元素数。

Capacity 是 ArrayList 可以存储的元素数。Count 是 ArrayList 中实际包含的元素数。Capacity 总是大于或等于 Count。如果在添加元素时,Count 超过 Capacity,则该列表的容量会通过自动重新分配内部数组加倍。

如果 Capacity 的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果 Capacity 被显式设置为
0,则公共语言运行库将其设置为默认容量。默认容量为 16

在调用Clear后,Count为0,而此时Capacity切是默认容量16,而不是0

3public virtual void TrimToSize();

将容量设置为 ArrayList 中元素的实际数量。

如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。

若要完全清除列表中的所有元素,请在调用 TrimToSize 之前调用 Clear 方法。截去空 ArrayList 会将 ArrayList 的容量设置为默认容量,而不是零。

ArrayList aList
= new ArrayList();

aList.Add(
"a");

aList.Add(
"b");

aList.Add(
"c");

aList.Add(
"d");

aList.Add(
"e");  //Count = 5,Capacity=16,

aList.TrimToSize(); 
//Count=Capacity=5;

Hashtable 类
一。介绍
   表示键 / 值对的集合,这些键 / 值对 根据键的哈希代码进行组织。
   提供快速的查询。元素的存储与顺序无关。不能在指定位置插入元素,因为它本身没有有效的排序。感觉它的优点体现在查询上。
   hashtable 的键必须是唯一的,没有有效的排序,它进行的是内在的排序
public class Hashtable : IDictionary, ICollection, IEnumerable,
   ISerializable, IDeserializationCallback, ICloneable
每个元素是一个存储在 DictionaryEntry 对象中的键
/ 值对。键不能为空引用( Visual Basic 中为 Nothing ),但值可以。
注:所谓的 DictionaryEntry 结构,就是定义可设置或检索的字典键值对,有一个 Key 属性, 一个 Value 属性,分别代表键和值 
二。添加
Hashtable.Add 方法
public virtual void Add(     object key,    object value );
将带有指定键和值的元素添加到 Hashtable 中。
key 要添加的元素的键。 value 要添加的元素的值。该值可以为空引用( Visual Basic 中为 Nothing )。
通过设置 Hashtable 中不存在的键的值, Item 属性也可用于添加新元素。例如: myCollection[
"myNonexistentKey"] = myValue 。但是,如 果指定的键已经存在于 Hashtable 中,设置 Item 属性将改写旧值。相比之下, Add 方法不 修改现有元素。
Hashtable hTable
= new Hashtable();
hTable.Add(
"1","a");
hTable.Add(
"2","b");
hTable.Add(
"3","c");
hTable.Add(
"4","d");
hTable.Add(
"5","e");
//hTable.Add("1","aaaaaaaaaaa");  // 出错,因为已经有键“ 1 ”
hTable["1"] = "aaaaaaaaaaa"//ok
DropDownList2.DataSource = hTable;
DropDownList2.DataTextField
= "Key";
DropDownList2.DataValueField
= "Value";
DropDownList2.DataBind();
二。删除
1public virtual void Remove(    object key );
从 Hashtable 中移除带有指定键的元素。
如果 Hashtable 不包含带有指定键的元素,则 Hashtable 保持不变。不引发异常。
2public virtual void Clear(); 从 Hashtable 中移除所有元素。
Count 设置为零。容量保持不变。
三。其他
1public virtual ICollection Values {get;}
获取包含 Hashtable 中的值的 ICollection 。
注: ICollection 接口,定义所有集合的大小、枚举数和同步方法。
Hashtable hTable
= new Hashtable();
hTable.Add(
"1","a");
hTable.Add(
"2","b");
hTable.Add(
"3","c");
hTable.Add(
"4","d");
hTable.Add(
"5","e");
Label2.Text
= "";
foreach (string strKey in hTable.Keys)  // 遍历
{
Label2.Text
+= strKey;
Label2.Text
+= ",";
}
2public virtual ICollection Keys {get;}
获取包含 Hashtable 中的键的 ICollection 。
返回的 ICollection 不是静态副本;相反, ICollection 反向引用原始 Hashtable 中的键。



因此,对 Hashtable 的更改继续反映到 ICollection 中。
3public virtual bool Contains(     object key );
确定 Hashtable 是否包含特定键。 
4public virtual bool ContainsKey(     object key );
确定 Hashtable 是否包含特定键。与 Contains 同 
5public virtual bool ContainsValue(    object value ) ;
确定 Hashtable 是否包含特定值
6public virtual int Count {get;}
获取包含在 Hashtable 中的键值对的数目  
Hashtable示例
<%@ Page Language="C#" AutoEventWireup="True" Debug="true" %>
<s cript language="C#" runat="server">
void Page_Load(Object Sender,EventArgs e){
Hashtable HT_values
=new Hashtable();
HT_values.Add(
"1234","Microsoft");
HT_values.Add(
"3210","IBM");
HT_values.Add(
"4321","SUN");
HT_values.Add(
"5623","Orlce");

MyDownList.DataSource
=HT_values;
MyDownList.DataValueField
="Key";
MyDownList.DataTextField
="Value";

MyListBox.DataSource
=HT_values;
MyListBox.DataValueField
="Key";
MyListBox.DataTextField
="Value";

MyDataGrid.DataSource
=HT_values;

Page.DataBind();
}
</s cript>
<form id="Form_1" runat="server">
<asp:dropdownlist ID="MyDownList" runat="server"></asp:dropdownlist><br>
<asp:listbox ID="MyListBox" runat="server"></asp:listbox><br>
<asp:datagrid ID="MyDataGrid" runat="server" AutoGenerateColumns="false">
<columns>
 
<asp:boundcolumn HeaderText="Key" DataField="Key"/>
 
<asp:boundcolumn HeaderText="Value" DataField="Value"/>
</columns>
</asp:datagrid>
</form>

转载于:https://www.cnblogs.com/sgivee/archive/2010/07/04/1771060.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值