Java Advanced for Loop

Source: Internet
Author: User

/* 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

Related Article

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.