A dictionary is a very complex data structure that allows elements to be accessed by a key, which can be any data type. A dictionary can also be called a ing or a hash. when you want to save the data as an array, but want to use other data types (not numeric) to index the structure, the dictionary is very suitable. they can also add or delete elements freely, a bit like arraylist.
If the dictionary is used? Assume that there is an employee class with the employee ID and the employee data items to record the identity and information of the employee. Here, the employee information only includes the name and salary.
Assume that the following employeeid exists:
Employeeid id = new employeeid ("m0001 ");
There is a variable employees. In syntax, we can regard this variable as an array of the employeedata object. but in fact, it is not an array, but a dictionary. Therefore, you can use the ID stated above to obtain employee information, as shown below:
Employeedata theemployee = employees [ID];
This is the dictionary function. it looks like an array (actually more powerful than this, it is more like an arraylist, because it can dynamically set its capacity, add or delete its elements ), however, you can use any data type to create an index instead of an integer. for dictionaries, this is called keys rather than indexes. roughly speaking, the dictionary uses this key when accessing the dictionary element. In the preceding example, this key is the ID object. you can also perform some processing on the value of this key. This processing will return an integer based on the key value to determine where the elements should be stored in the array, or from somewhere. other scenarios where the dictionary is used to store objects include:
- Employees or other people's information can be stored, and their Social Security numbers can be used as indexes. the social security number is basically an integer, but an array cannot be used and used as an index. Theoretically, the maximum number is 999999999, which is impossible on 32 systems.ProgramAllocate such a large address space to the array. Most of the arrays are empty. Instead, you can use the social security number to create indexes for employees, and the dictionary occupies less space.
- If you want to store the address, the index is a zip code. in China, the postal code is a number, but in Canada, the Postal Code also contains letters. in UK, the zip code is a string containing letters and numbers.
- The index of the data of any object or person to be stored is the name of the object or person.
Although the function of the dictionary is to make the clientCodeIt looks more like an employee's dynamic array and has a very flexible indexing mechanism, but the background has to do a lot of work to achieve this function. in general, any class object can be used as the dictionary index key, but before that, you need to execute some functions on the class, usually to execute gethashcode (), all classes and structures are from system. the object inherits this method.