Common For loop writing and optimization

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

At the same time, I participated in the Digitalstar project to win the creative gift package and challenge the creative incentive money

The introduction

We all often use some operations to calculate the cycle time, especially for loop, which is a kind of repeated calculation operation. If it is not handled properly, the time will be relatively large. If it is handled properly, the efficiency will be greatly improved. First, we initialize a collection list as follows:

Method 1: the most conventional writing method without thinking

for (int i = 0; i < list.size(); i++) {
 System.out.println(list.get(i));
}
Copy the code
  • Advantages: More common, easy to understand
  • Disadvantages: Evaluates list.size() every time

Method two: extract the length of the array

int m = list.size();
for (int i = 0; i < m; i++) {
   System.out.println(list.get(i));
}
Copy the code

Advantages: You don’t have to count the disadvantages every time:

  • The scope of M is not small enough, violating the principle of minimum scope;
  • You cannot manipulate the size of a list in a for loop, such as removing or adding an element

Method three: extract the length of the array

for (int i = 0, n = list.size(); i < n; i++) {
    System.out.println(list.get(i));
}
Copy the code

Advantages: It is not necessary to calculate every time, and the scope of variables follows the principle of minimum scope. Disadvantages:

  • The scope of M is not small enough, violating the principle of minimum scope;
  • You cannot manipulate the size of a list in a for loop, such as removing or adding an element

Method four: write in reverse order

for (int i = list.size() - 1; i >= 0; i--) {
    System.out.println(list.get(i));
}
Copy the code

Advantages: It is not necessary to calculate every time, and the scope of variables follows the principle of minimum scope. Disadvantages:

  • The order of results would be reversed;
  • It looks unaccustomed and difficult to read

Application: Not related to the order in which the results are displayed: for example, to save the validation of previous data

Method 5: Iterator traversal

for (Iterator<String> it = list.iterator(); it.hasNext();) {
    System.out.println(it.next());
}
Copy the code

Pros: Simplicity

If you add or remove elements will be directly submitted to the Java. Util. ConcurrentModificationException anomalies

Method six: jdK1.5 after writing

for (Object o : list) {
     System.out.println(o);
}
Copy the code

Advantages: Brevity combined with generics. Disadvantages: JDK1.4 downward incompatibility

Method 7: Lamada writing

list.forEach(i-> System.out.println(i));
Copy the code

Advantages: concise code disadvantages: poor readability

Method seven: loop nesting outside small inside big principle

for (int i = 0; i < 10; i++) {
   for (int j = 0; j < 10000; j++) {
     // Logical processing}}Copy the code

The reason:

Method 8: Loop nested extraction does not require loop logic

/ / before:
 int a = 10, b = 11;
  for (int i = 0; i < 10; i++) {
      i = i * a * b;
  } 
 
 After the / / :
 int c = a * b;
 for (int i = 0; i < 10; i++) {
     i = i * c;
  }
Copy the code

Method 9: Exception handling is written outside the loop

counter-examples

for (int i = 0; i < 10; i++) {
     try{}catch (Exception e) {
 
     }
}
Copy the code

Is case

try {
   for (int i = 0; i < 10; i++) {
   }
} catch (Exception e) {
 
}
Copy the code