This chapter focuses on the importance of a suitable data structure to a program. A good data type can save development time and save on memory consumption.
At the end of this article summarize four points:
1. Rewrite the duplicate code using an array. In the current become the main use is the list, map and other data structures to replace the array.
2. Encapsulate complex structures. Abstract Bean.
3. Use advanced tools whenever possible. Hypertext, name-value pairs, spreadsheets, databases, programming languages, and more are powerful tools in specific problem areas. As the book is published earlier, the basic point mentioned here is the need for daily development. Compare new tools Redis has just come into contact with the current project. Data is also stored in the form of key-value pairs.
4. Derive the structure of the program from the data. The topic of this chapter is that by using the appropriate data structure instead of the complex code, the results of the program can be derived from it. Original aim: Before writing code, good programmers thoroughly understand input, output, and intermediate data structures and create programs around those structures.
Exercises:
1. Optimize the following code
1 if(income<=2200)2Tax=0;3 Else if(income<2700)4tax= 0.14* (income-2200);5 Else if(Income <=3200)6Tax = + 0.15* (income-2700);7 Else if(Income <=3200)8Tax = 145 + 0.16 * (income-3200);9 Else if(Income <=4700)TenTax = 225 = 0.17 * (income-3700); One ... A Else -Tax = 53090 + 0.70 * (income-102200);
Answer: Although a result is given, there is still a problem.
1. There are 25 if judgments in this, so the increment of interest rate is greater than 0.01 after that, so it is incorrect to follow this notation. You might consider using arrays instead of these increments and doing loops.
1 int number = (income-2200)%500; 2 for (int inti=0;inti<=number+1;inti++) {3 moneyrate=0.14+0.1*IntI; 4 Redis= (income-2200) -500*IntI; 5 tax=tax+moneyrate*Redis; 6 )
< Span style= "color: #000000;" > optimized
1 New ArrayList (0.14,0.15,0.16,0.17,0.18,...., 0.70); 2 int number = (income-2200)%500+1; 3 for (int inti=0;inti<moneyrates.length && inti<=number; inti++) {4 redis= (income-2200) -500*IntI; 5 tax=tax+moneyrates[inti]*Redis; 6 }
Chapter III Data Determination of program structure