For (String s: v), forstring
S is the variable assigned after traversal, and v is the list to be traversed.
You can use the following statement to test:
List <String> v = new ArrayList ();
V. add ("one ");
V. add ("two ");
For (String s: v ){
System. out. println (s );
}
Note that s must match the preceding type.
From jim_yl Baidu
For (String s: str) is what?
Equivalent
For (int I = 0; I <str. length; I ++ ){
String s = str [I];
System. out. print (s );
}
Is another form of for loop provided by J2SE 1.5, which can be used to handle cases where the str length is unknown.
In java, for (String str: s) {}. How can this problem be solved?
This statement enhances the for loop,
For (int I = 0; I <s. length (); I ++ ){
String str = s [I]; // used as an array
}
The compiler will think:
1. Create a String variable named str.
2. Assign the first element of s to str.
3. Execute duplicate content.
4. assign a value to the next element 'str.
5. Repeat until all elements are run.
Advantages:
This method makes our code look more concise.
Disadvantages:
1. Only all elements can be traversed sequentially, and complicated loops cannot be implemented.
2. For arrays, you cannot easily access the lower tag value;
3. Compared with Interator, you cannot conveniently Delete the content in the Set (Interator is also called internally ).
4 In addition to simple traversal and reading of the content, we do not recommend using an enhanced for loop.