J2SE5.0 New Features of Foreach
晁 晁 攀 Smallnest@163.com
Foreach is available in C #:
Foreach (String Item IN f) {console.writeline (item);
Java also added such features:
Package com.kuaff.jdk5; import java.util. *; import java.util.collection; public class foreach {private collection
For collection types and array types, we can access it through Foreach syntax. In the above example, we have to access arrays in turn, it is troubles:
For (int i = 0; i Now just need the following simple statement: For (String B: belle) {system.out.println ("Once you've ate:" b); More visiting the collection is more obvious. I used to access the collection code: ITERATOR IT = C.ITerator (); it.hasnext ();) {string name = (string) it.next (); system.out.println ("Once Week:" Name); Now we only need the following statement: For (String B: C) {System.out.println ("Once Week:" B); Foreach is not universal, it also has the following shortcomings: In the previous code, we can perform the REMOVE operation via the Iterator. Iterator it = c.iterator (); it.hasnext ();) { iTremove () } However, in the current Foreach version, we cannot delete the objects contained in the collection. You can't replace objects. At the same time, you cannot parallel with more than multi-collection. So, when we write the code, you have to use it.