/* The advanced for loop is a shorthand for the (Collection) iterator in the collection. That is, iterators in the collection can be replaced with advanced for. Format: for (data type variable name: Facilitated collection (Collection) or array) {} Advanced for loop only iterates over the collection. Only collection elements can be obtained, but the collection cannot be manipulated. In addition to iterating, iterators can also act on elements in the Remove collection. If you use Listiterator, you can also add and remove elements from the collection. What is the difference between a traditional for loop and an advanced for loop? The advanced for Loop has a limitation and must have a target to be traversed, and it is recommended to use the traditional for when iterating over the array. Because the traditional for can define a corner label. */import java.util.*;class Foreachdemo {public static void main (string[] args) {/*arraylist<string> al=new Arraylist<string> (); Al.add ("ABC1"); Al.add ("ABC2"); Al.add ("ABC3"); for (String S:al) SOP (s); int[] arr={ 1,3,4,5,2};for (int I:arr) SOP ("I:" +i);*/hashmap<integer,string> hm=new hashmap<integer,string> (); Hm.put (1, "Hello1"), Hm.put (3, "Hello3"), Hm.put (2, "Hello2"), Hm.put (4, "Hello4"); Set<integer> Set=hm.keyset (); for (Integer I:set) sop (i+ "...") +hm.get (i)); Set<map.entry<integer,string>> Set1=hm.entryset (); for (map.entry<integer,string> Me:set1) { Integer Key=me.getkey (); String value=me.getvalue (); SOP (key+ "..." +value);} public static void Sop (Object obj) {System.out.println (obj);}}
Java Advanced for Loop