Package COM. LC. day01.forenhance; import Java. util. arraylist; import Java. util. iterator; import Java. util. linkedhashmap; import Java. util. list; import Java. util. map; import Java. util. map. entry; import Java. util. set; import Org. JUnit. test; public class forenhance {@ testpublic void arrayforenhance () {int arr [] = {1, 2, 3}; For (INT num: ARR) {system. out. println ("num") ;}@testpublic void listforenhance () {list = new arraylist (); list. add (1); list. add (2); list. add (3); list. add (4); For (Object OBJ: List) {int I = (integer) OBJ; system. out. println (I) ;}}/** 1. hashmap () is saved based on the algorithm. use linkedhashmap () */@ testpublic void mapforenhance () {// map = new hashmap (); // map = new linkedhashmap () Saved by the algorithm; // store the map in the order it is stored. put ("1", "AAA"); map. put ("2", "BBB"); map. put ("3", "CCC"); map. put ("4", "fff"); map. put ("5", "rrr");/** 1. traditional method 1 */set = map. keyset (); iterator it = set. iterator (); While (it. hasnext () {string key = (string) it. next (); string value = (string) map. get (key); system. out. println (Key + "=" + value);} system. out. println ("------------------------");/** 2. traditional method 2 */set set1 = map. entryset (); iterator it1 = set1.iterator (); While (it1.hasnext () {map. entry entry = (entry) it1.next (); string key = (string) entry. getkey (); string value = (string) entry. getvalue (); system. out. println (Key + "=" + value);} system. out. println ("------------------------");/** 3. use the enhanced for loop 1 */For (Object OBJ: map. keyset () {string key = (string) OBJ; string value = (string) map. get (key); system. out. println (Key + "=" + value);} system. out. println ("------------------------");/** 4. use the enhanced for loop 2 */For (Object OBJ: map. entryset () {map. entry entry1 = (entry) OBJ; string key = (string) entry1.getkey (); string value = (string) entry1.getvalue (); system. out. println (Key + "=" + value) ;}@ test/** note the following when using an enhanced for loop: the enhanced for loop is only suitable for obtaining data */Public void forenhancepoint () {int arr [] = {1, 2, 3}; For (int I: ARR) {I = 10 ;} system. out. println (ARR [0]); system. out. println (ARR [1]); system. out. println (ARR [2]); List list = new arraylist (); list. add ("1"); For (Object OBJ: List) {OBJ = "10";} system. out. println ("--------------------"); system. out. println (list. get (0 ));}}
Enhanced for cyclic learning notes