Use a set to organize relevant data and a set to organize relevant data
Set Overview
An array is an upgraded version. It can dynamically define and maintain the length of the Set (that is, the maximum number of elements in the set!
ArrayList
ArrayList is very similar to an array. It is also called an array list. Its capacity can be dynamically expanded as needed, and its indexes will be re-allocated and adjusted based on the expansion of the Set capacity. That is to say, the subscript of the element in the ArrayList set is uncertain and variable.
The ArrayList class belongs to System. collections namespace, which contains interfaces and classes that define a set of objects (such as lists, queues, bit arrays, hash tables, and dictionaries.
Syntax:Using System. Collections; // import the namespace // define the ArrayList object ArrayList set name = new ArrayList ([length ]);
Common methods and attributes of ArrayList
Attribute name |
Description |
Count |
Obtains the number of actually contained elements in the ArrayList. |
Return Value Type |
Method Name |
Description |
Int |
Add (Object Value) |
Add the object to the end of ArrayList |
Void |
RemoveAt (int index) |
Removes the element at the specified index in the ArrayList. |
Void |
Remove (Object Value) |
Remove a specific object from ArrayList |
Void |
Clear () |
Remove all elements from ArrayList |
HashTable
In ArrayList, we can access the elements in the set through indexes. However, when the element indexes in the set change frequently, It is very troublesome to find the position (INDEX) of each element,
C # provides a data structure called HashTable. Generally, it is called a hash table and someone calls it a "Dictionary". It is called a dictionary because it is very similar to a dictionary, all of them use a word to find more information about the word. HashTable organizes data by Key pair Value.
Common attributes and methods of HashTable
Attribute |
Description |
Count |
Obtain the number of key-value pairs contained in HashTable. |
Keys |
Obtain the set containing the HasTable key. |
Values |
Obtain the value set that contains HasTable. |
Return Value Type |
Method Name |
Description |
Void |
Add (Object Key, Object Value) |
Add the element with the specified key and value to HashTable. |
Void |
Remove (Object Key) |
Remove elements with specific keys from HashTable |
Void |
Clear () |
Clear all elements in HashTable |
Generic and generic Sets
Generic and generic Sets
Data stored through ArrayList and HashTable are converted to the Object type, which means that it can store different types of elements in a collection, when traversing a set, the forced type conversion error may occur. Generic collections are type-safe. The element types in the set are defined first. The following describes the generic and generic sets in detail.
Generic
Generic is a new feature in C #2.0. Generic introduces a concept: type parameters, by using type parameters (T) this reduces the risk of forced type conversion or packing and unpacking during running. Code can be reused to the maximum extent, protecting type security and improving performance, its most common application is to create a collection class, which can constrain the element type in the Collection class. Typical generic collections are LIst <T> and Dictionary <K, V>,
Generic set
The System. Collections. Generic namespace defines multiple Generic collection classes. These classes can replace the preceding ArrayList
The syntax for defining a List <T> set is as follows:
List <T> set name = new List <T> ();
T in "<T>" can constrain the element types in the set. T table names are the element types managed by the set.
List <T> and ArrayList differences generic set Dictionary <K, V>
The generic set Dictionary can replace the HashTable
Defines the syntax of a generic set Dictionary <K, V>
Dictionary <K, V> set name = new Dictionary <K, V> ();
In "<K, V>", K indicates the type of the Key in the Set, and V indicates the type of Value. Their meanings are the same as those in List <K, V>.
Generic Type
The use of generics in a collection is only one of a variety of generic applications, and there are already generic applications in terms of classes and methods.
The syntax for defining a generic class is as follows:
Public class name <T> {//....}
T indicates the type parameter, which indicates the specific data type. It can be a value type or a reference type.