Enhanced for cyclic learning notes

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.