Thinking about the table driving method

Source: Internet
Author: User

currently in the study of software construction curriculum of the table-driven method, after understanding that it is very useful in dealing with multiple if-else problems, here are some simple I understand the table-driven method and its application in Java small example.

Table-driven method, as the name implies, is to use a table to obtain data, involving the structure of the table. Tables are a common data structure in many languages, such as in Java, where we use the map key values to define a table as a collection. The usage of map in Java is as follows:

New HashMap (); Delete: Map.clear (); Add key-value pairs: Map.set (key,value); Remove the value of the key: Map.get (key) loops through all the key-value pairs:  for (Object obj:map.keySet ()) {map.get (obj);}

Table-driven lookup methods include direct access, index access, and ladder access.

Direct access is done by the subscript directly to find the corresponding data content, this way generally only need to use the data structure of the array. Here is a simple example:

Static String []week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Week Seven"}; int daytime = 3= week[daytime--]; System.out.print (Dayname);

Dayname = "Wednesday";

The way index access is used in our daily life to find simple tables of the scene, looking for the condition is not intuitive. For example, we want to simulate a Chinese dictionary through the program, find pinyin as "Hao" of Chinese characters, pinyin "Hao" is a string, no dictionary page number form intuitive, can not be used as an array subscript, in Java at this time use map more appropriate, pinyin as the key, the value of the corresponding Chinese characters and so on. Here is another simple example:

 static  String []week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Week Seven" }; static  String []plan = {"Learning course", "Practice Baseball", "Piano playing", "Outdoor Sketching", "Picnic with Family", "go shopping", "tidy room"  static  map<string,string> Dayplan = new   HashMap ( ); for  (int  i =0;i<week.length;i++ =dayplan.get ("Wednesday" ); System.out.print (Wednesdayplan);  // wednesdayplan = "Piano playing"  


       Finally, ladder access, also known as segmented access, is the process of locating data in a table and then finding it based on the upper segment limit. The application in our daily life is more common is the point of merit query. For example, if you have a course score of 82, you would like to check the GPA of the course exam results, you will be in two tables. First, you'll need to find the upper limit of the score that is closest to your score in the table with the upper limit of the score points: 84, and then find the corresponding performance point at 3.3 by the upper limit of 85 points. The following is the source code:

 static  int  []range = { 59,64,74,84,89,100 static  Double []gpa = {0.,1.,2.,3.3,3.7,4 double  GPA = 0;  int  grades = 82;  for  (int  i=0;i<range.length;i++) { if  (Grades<=range[i]) {GPA  = Gpa[i];      break  ; }}system.out.print (GPA);  // gpa = 3.3  

That ' s All~thank you!


Thinking about the table driving method

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.