Dictionary implementation (2)-Using sequential tables (arrays) to implement dictionaries

Source: Internet
Author: User

One, the idea

1, the Dictionary realization class Arraydictionary.java must implements the Dictionary interface Dictionaryinterface.java (reference: http://www.cnblogs.com/hapjin/p/4573826.html). Second, in order to serialize, but also to implements serializable interface

2, because each element in the dictionary is a key-value pair. Therefore, the lookup key and value need to be encapsulated in the same object, so Entry.java is defined to represent each element in the dictionary. Among them, the entry class is implemented as the inner class of the arraydictionary.

3, because each entry object represents a dictionary element, Arraydictionary.java requires an array of entry types to hold the dictionary elements. This is also the title of the article-the use of sequential tables to achieve the cause of the dictionary.

In further discussion, we know that Arraylist.java in the Java class Library is equivalent to a sequential table (array) that has been implemented for programmers, and we do not need to pay attention to the specifics of the implementation of the array and how to iterate over the array, but instead use the various methods in the Arraylist.java directly ( Add,remove ...) To complete the basic operation of the array, the iterator that iterates through the array is obtained through the iterator () method. You can also use the Arraylist.java method to assist in implementing the methods defined in the Dictionaryinterface interface.

However, in this case, a custom array is used to implement the dictionary, so the method in Arraydictionary.java needs to be done according to the methods in the Dictionaryinterface interface and the needs of the self.

Below, specifically to clarify the implementation of the idea of the iterator: in arraydictionary. Java, three private inner classes are defined to represent implementations of three iterators:

Class Entryiterator<entry> iterator that iterates through the Entry array

Class Keyiterator<k> iterator that implements the traversal lookup key

Class Valueiterator<v> iterator that implements the value

Where the latter two iterators use iterators that traverse the entry array, because the key (key) and values (value) are encapsulated in the entry.

At the same time, the Arraydictionary.java defines three corresponding methods of acquiring the ergodic device to obtain the above three ergodic.

To be Continued ...

Dictionary implementation (2)-Using sequential tables (arrays) to implement dictionaries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.