---------------------- ASP. NET + Android + I/O Development S,. Net training, hope to communicate with you! ----------------------
Arraylist Variable Length array, using an array similar
Property capacity (the number of elements that can be accommodated in a set doubles); count (the number of actually stored elements in the set .)
Method
Add () addrange (icollection c) Remove () removeat () Clear ()
Contains () toarray () sort () Sort \ reverse (); // reverse
A collection of hashtable key-value pairs, similar to a dictionary. hashtable is fast in searching for elements.
Add (Object key, object value );
Hash ["key"]
Hash ["key"] = "modify ";
. Containskey ("key ");
Remove ("key ");
Traversal:
Hash. Keys
Hash. Values/dictionaryentry
Key-value pairs in the set must be unique.
Arraylist (non-generic) → list <t> (generic)
Hashtable (non-generic) → dictionary <K, V> (generic)
The gethashcode method is applicable to hash algorithms and data structures such as hash tables.
The default implementation of the gethashcode method does not guarantee that unique values are returned for different objects. In addition, the. NET Framework does not guarantee the default implementation of the gethashcode method and the value it returns is the same in different versions of. NET Framework. Therefore, the default implementation of this method cannot be used as a unique object identifier during hash operations.
The gethashcode method can be rewritten by a derived type. The value type must be overwritten to provide hash functions suitable for this type and to provide useful distribution in the hash table. To get the best result, the hash code must be based on the value of the Instance field or attribute (instead of the static field or attribute.
The gethashcode method must be rewritten for the objects used as the middle keys of the hashtable object, because these objects must generate their own hash code. If the object used as the key does not provide the useful implementation of gethashcode, you can specify the hash code provider when constructing the hashtable object. Before. NET Framework 2.0, the hash code provider was based on the system. Collections. ihashcodeprovider interface. Starting from version 2.0, the hash code provider is based on system. Collections. iequalitycomparer
Interface.
Description of implementers:
The hash function is used to quickly generate a number (hash code) corresponding to the object value ). Hash Functions are generally specific to each type, and at least one instance field must be used as the input.
Hash functions must have the following features:
If the comparison results of the two objects are the same, the gethashcode method of each object must return the same value. However, if the comparison results of the two objects are not equal, the gethashcode method of the two objects may not necessarily return different values.
The gethashcode method of an object must always return the same hash code, provided that the object state has not been modified. The object state is used to determine the return value of the equals method of the object. Note that this only applies to the current execution of the application. When you run the application again, another hash code may be returned.
To achieve optimal performance, the hash function must generate a random distribution for all input.
For example, the gethashcode method provided by the string class returns the same hash code for the same string value. Therefore, if two string objects represent the same string value, they return the same hash code. In addition, this method uses all characters in the string to generate distributed output that is quite random, even if the input is concentrated within a certain range (for example, many users may have strings containing only 128 ASCII characters, even if the string can contain any of the 65,535 Unicode characters ).
For a derived class of an object, the gethashcode method can be delegated to object. gethashcode only when this derived class defines equality of values as references and the type is not a value type.
Providing a good hash function on the class can significantly affect the performance of adding these objects to the hash table. In a hash table with good hash functions, the time used for searching elements is fixed (for example, an O (1) operation ). In a hash table with poor hash functions, the search performance depends on the number of items in the hash table (for example, the computing complexity is O (n, where N is the number of items in the hash table ). The computing cost of hash functions must also be low.
The implementation of the gethashcode method must not cause circular reference. For example, if classa. gethashcode calls classb. gethashcode, classb. gethashcode must not directly or indirectly call classa. gethashcode.
The implementation of the gethashcode method must not cause an exception.
The derived class that overrides gethashcode must also override equals to ensure that the two objects considered to be equal have the same hash code; otherwise, the hashtable type may not work properly.
// View the Running Speed
Stopwatch Sw = new stopwatch ();
Sw. Start ();
Mydic. containskey ("China ");
Sw. Stop ();
MessageBox. Show (SW. elapsed. tostring ());
Class keypair
{
Private string key;
Public String key
{
Get {return key ;}
Set {key = value ;}
}
Private string value;
Public String Value
{
Get {return this. value ;}
Set {This. value = value ;}
}
}
Class mydict
{
List <keypair> List = new list <keypair> ();
Public void add (keypair KP)
{
List. Add (KP );
}
Public bool containskey (string key)
{
For (INT I = 0; I <list. Count; I ++)
{
If (list [I]. Key = key)
{
Return true;
}
}
Return false;
}
}
Array is the parent class of all arrays.
The arraylist set can store any type of data,
However, no matter what type of data is put into the arraylist, it becomes the object type.
Therefore, the data retrieved from the arraylist needs to be converted by type.
Do not put the random instantiation in the loop!
You can use two sets to reduce the number of cycles that generate random numbers.
Random reduces the execution efficiency in a loop (the seeds are the same each time new, the current time .)
Namespace system. Collections. Generic
List <t> is similar to the upgraded version of arraylist and arraylist.
Various methods: Sort (), max (), min (), sum ()...
Dictionary <K, V> is similar to the upgraded version of hashtable and hashtable.
Generic set is recommended.
T, K, and V are like a lock. The lock set can only store certain types of data. Here, T, K, and V can be other letters.
You can perform foreach traversal on a generic set because ienumerable <t> has the getenumerator () method.
Arraylist al1 = new arraylist () {"A", "B", "C", "D", "E "};
Arraylist Al = new arraylist () {"D", "E", "F", "g", "H "};
Arraylist _3 = new arraylist ();
Al3.addrange (al1 );
Foreach (string STR in Al)
{
If (! Al3.contains (STR ))
{
Al3.add (STR );
}
}
Foreach (string STR in A0)
{
Console. writeline (STR );
}
======================================
Arraylist Al = new arraylist ();
Random ran = new random ();
While (Al. Count <10)
{
Int num = ran. Next (1, 31 );
If (Num % 2 = 0)
{
// If the set does not contain num
If (! Al. Contains (Num ))
{
Al. Add (Num );
}
}
}
========================================================
String S = "5 8 9 10 11 22 4 3 ";
Arraylist al1 = new arraylist ();
Arraylist Al = new arraylist ();
String [] arr = S. Split ('');
Foreach (string item in ARR)
{
Int n = int. parse (item );
If (N % 2! = 0)
{
Al1.add (N );
}
Else
{
Al2.add (N );
}
}
Al1.addrange (A2)
-------------------- ASP. NET + Android + iOS development,. Net training, and hope to communicate with you! ----------------------
See http://edu.csdn.net for details