The System.Collections.ArrayList class is a special array. By adding and removing elements, you can dynamically change the length of the array.
A Advantages
1. Supports auto-size-changing features
2. Flexibility to insert elements
3. The ability to remove elements flexibly
Two Limitations
It's a little bit faster than a normal array.
Three adding elements
1. public virtual int Add (object value);
Add an object to the end of the ArrayList
ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Content is a b c d E
2. public virtual void Insert (int index,object value);
Inserts an element at the specified index of the ArrayList
ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.insert (0, "AA");
The result is a AA a B c d E
3. public virtual void Insertrange (int index,icollection c);
Inserts an element from the collection at the specified index of 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);
The result is a B TT ttt C d E
Four Delete
A) public virtual void Remove (object obj);
Remove the first occurrence of a specific object from ArrayList, note that the first
ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.remove ("a");
The result is b C D E
2. Public virtual void RemoveAt (int index);
Removes the element at the specified index of the ArrayList
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.removeat (0);
The result is b C D E
3. public virtual void RemoveRange (int index,int count);
Removes a range of elements from the ArrayList.
Index, which represents the number that starts at the index
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.removerange (1,3);
The result is a E
4. public virtual void Clear ();
Removes all elements from the ArrayList.
Five Sort
A) public virtual void Sort ();
Sorts the elements in a ArrayList or part of it.
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 ();
The result is 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 (); Sort
Dropdownlist1.datasource = alist; DropDownList DropDownList1;
Dropdownlist1.databind ();
The result is a B c d E
b) public virtual void Reverse ();
Reverses the order of the elements in the ArrayList or part of it.
ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.reverse (); Reverse
Dropdownlist1.datasource = alist; DropDownList DropDownList1;
Dropdownlist1.databind ();
The result is e D C b A
Six Find
A) public virtual int IndexOf (object);
b) public virtual int IndexOf (object, int);
c) public virtual int IndexOf (object, int, int);
Returns the zero-based index of the first occurrence of a value in ArrayList or part of it. No return-1 found.
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"); Not found,-1
d) public virtual int LastIndexOf (object);
e) public virtual int LastIndexOf (object, int);
f) Public virtual int LastIndexOf (object, int, int);
Returns the zero-based index of the last occurrence of a value in the ArrayList or part of it.
ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("a"); Same 0
Alist.add ("D");
Alist.add ("E");
int nIndex = Alist.lastindexof ("a"); Value is 2 instead of 0
g) Public virtual bool Contains (object item);
Determines whether an element is in ArrayList. Contains return true, otherwise returns false
Seven Other
1. public virtual int capacity {get; set;}
Gets or sets the number of elements that ArrayList can contain.
2. public virtual int Count {get;}
Gets the number of elements actually contained in the ArrayList.
Capacity is the number of elements that ArrayList can store. Count is the number of elements actually contained in ArrayList. Capacity is always greater than or equal to Count. If Count exceeds capacity when the element is added, the capacity of the list is doubled by automatically reallocating the internal array.
If the value of capacity is explicitly set, the internal array also needs to be reassigned to accommodate the specified capacity. If capacity is explicitly set to 0, the common language runtime sets it to the default capacity. The default capacity is 16.
When clear is called, Count is 0, and at this point capacity is the default capacity of 16 instead of 0
3. public virtual void trimtosize ();
Sets the capacity to the actual number of elements in ArrayList.
If you do not add new elements to the list, this method can be used to minimize the memory overhead of the list.
To completely clear all the elements in the list, call the Clear method before you call TrimToSize. Truncating the empty ArrayList will set the ArrayList capacity to the default capacity, not zero.
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 class
One. Introduced
Represents a collection of key/value pairs that are organized according to the hash code of the key.
Provides quick queries. The storage of elements is independent of order. The element cannot be inserted at the specified location because it does not have a valid sort by itself. Feel that its advantages are reflected in the query.
The Hashtable key must be unique, without a valid sort, and it carries the inner sort
public class Hashtable:idictionary, ICollection, IEnumerable,
ISerializable, Ideserializationcallback, ICloneable
Each element is a key/value pair stored in the DictionaryEntry object. The key cannot be a null reference (Nothing in Visual Basic), but the value is OK.
Note: The so-called dictionaryentry structure is defined as a dictionary key-value pair that can be set or retrieved, with a key property, a Value property that represents the key and the value, respectively
Two. Add to
Hashtable.add method
public virtual void Add (object key, object value);
Adds an element with the specified key and value to the Hashtable.
Key of the element to add. Value of the element to add. The value can be a null reference (Nothing in Visual Basic).
The Item property can also be used to add a new element by setting the value of a key that does not exist in Hashtable. For example: mycollection["myNonexistentKey"] = myvalue. However, if the specified key already exists in Hashtable, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.
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"); Error because there is already a key "1"
htable["1"] = "aaaaaaaaaaa"; Ok
Dropdownlist2.datasource = htable;
Dropdownlist2.datatextfield = "Key";
Dropdownlist2.datavaluefield = "Value";
Dropdownlist2.databind ();
Two. Delete
1. public virtual void Remove (object key);
Removes the element with the specified key from the Hashtable.
If the Hashtable does not contain an element with the specified key, the Hashtable remains unchanged. No exception is thrown.
2. public virtual void Clear (); Removes all elements from the Hashtable.
Count is set to zero. The capacity remains the same.
Three. Other
1. Public virtual ICollection Values {get;}
Gets the ICollection that contains the value in Hashtable.
Note: The ICollection interface defines the size, enumerator, and synchronization methods for all collections.
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)//traversal
{
Label2.Text + = strkey;
Label2.Text + = ",";
}
2. Public virtual ICollection Keys {get;}
Gets the ICollection that contains the keys in the Hashtable.
The returned ICollection is not a static copy; instead, ICollection references the keys in the original Hashtable.
As a result, changes to Hashtable continue to be reflected in the ICollection.
3. Public virtual bool Contains (object key);
Determines whether the Hashtable contains a specific key.
4. Public virtual bool ContainsKey (object key);
Determines whether the Hashtable contains a specific key. Same as Contains
5. Public virtual bool Containsvalue (object value);
Determine if Hashtable contains a specific value
6. public virtual int Count {get;}
Gets the number of key-value pairs contained in the Hashtable
Hashtable example
<%@ 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>
Go ArrayList and Hashtable use