The old foreach of reinforces the distinction between for loops and for loops

  • 2020-10-07 18:42:40
  • OfStack

First of all, foreach also known as enhanced for loop, foreach is a special simplified version of for loop.

Again, the writing format of foreach:

for (element type element name: traversal group (set) (or iterated)) {

statements

}

Although foreach is a simplified version of for cycle, it does not mean that foreach is any more useful than for. foreach is more efficient when the number of cycles is unknown or when counting cycles is troublesome. However, more complex 1 cycles still require for to be more efficient.

Let's look at the following example:


public static void main(String[] args) {
  List<String> arr = new ArrayList<String>();
  arr.add(" hello ");
  arr.add(" I am good ");
  arr.add(" Hello, everyone ");
  
  //foreach cycle 
  for(String str : arr){       // Here, str That's just to get each cycle arr The values in the 
   System.out.println(str);       // Is equivalent to  String str=arr[i]
  }
 }

But the for loop is a little more cumbersome


public static void main(String[] args) {
  List<String> arr = new ArrayList<String>();
  arr.add(" hello ");
  arr.add(" I am good ");
  arr.add(" Hello, everyone ");
  
  //for cycle 
  for(int i=0;i<arr.size();i++){
   System.out.println(arr.get(i)); // To obtain a list The element of theta needs to be used get methods  
  }
 }

In addition to this common collection, you can also use key and value pairs like map

Such as:


public static void main(String[] args) {
  Map<String,String> mapstr = new HashMap<String,String>();
  mapstr.put(" The king ", " male ");
  mapstr.put(" li ", " male ");
  mapstr.put(" zhang ", " female ");
                      //entrySet The method is used to get a collection of key-value pairs 
  for(Map.Entry<String, String> s : mapstr.entrySet()){ // Here, Map.Entry<String, String> In fact, is 1 A type of   The type used to represent a key-value pair 
   System.out.println("key="+s.getKey());   // This is still the same thing  s=maostr.entrySet , only s It stores key-value pairs. 
   System.out.println("value="+s.getValue());  // So you can use get Method gets the stored key-value pairs. 
  }
 }

In addition, foreach does not support adding delete operations to loops, because when using foreach loops, arrays (collections) are already locked and cannot be modified, otherwise an java.util.ConcurrentModificationException exception will be reported

Such as:


public static void main(String[] args) {
  List<String> arr = new ArrayList<String>();
  arr.add(" hello ");
  arr.add(" I am good ");
  arr.add(" Hello, everyone ");
  
  //foreach cycle 
  for(String str : arr){     
   System.out.println(str);   
   arr.add("1");     // right arr add  
  }
 }

We'll talk more about how you can't add and delete in foreach next time.

So to summarize:

foreach is good for just collection or array traversal, while for is more efficient in more complex loops.

foreach cannot modify arrays or collections (add and delete operations), and if you want to modify them, you use for loops.

So the for cycle is more flexible than the for cycle.


Related articles: