Summarize for loop and for loop enhanced traversal arrays, List,set and map

Source: Internet
Author: User

One. For collections

(1) Normal for loop

        int [] arr = {2, 1, 2 };          for (int i=0;i<arr.length;i++) {            System.out.println (arr[i]);        }

(2) For loop enhancement

        int [] arr = {2, 1, 2 };          for (int  i:arr) {            System.out.println (i);        }        System.out.println ("********************");         // two-dimensional arrays        int [][]arr1={{2,1,3},{1,2,5}};          for (int  []i:arr1) {            for(int  j:i) {                System.out.println (j);            }        }

Two. For list,list is an interface, there are many classes that implement it, such as Arraylist,linkedlist,vector.

(1) using normal for loops and iterators

List<string> list=NewArraylist<string>(); List.add ("AAA"); List.add ("BBB"); List.add ("CCC"); //Normal Traversal         for(intI=0;i<list.size (); i++) {System.out.println (List.get (i)); } System.out.println ("****************"); //using iteratorsIterator iterator=List.iterator ();  while(Iterator.hasnext ()) {System.out.println (Iterator.next ()); }

(2) Use for loop enhancement

         for (String s:list) {            System.out.println (s);        }

Three. For the set interface, there are many classes that implement it, HashSet (unordered), Linkedhashset (ordered)

(1) Use iterators. Note that set does not have a get () Method!!!

        set<string> set=New hashset<>();        Set.add ("a");        Set.add ("B");        Set.add ("C");        Iterator Iterator=set.iterator ();          while (Iterator.hasnext ()) {            System.out.println (Iterator.next ());        }

(2) Use for loop enhancement

         for (String s:set) {            System.out.println (s);        }

Four. For the map interface, there are many classes that implement it, hashmap,linkedhashmap,hashtable. Store as a key-value pair

(1) The use of iterators, there are two ways

Map<string,string> map=NewHashmap<>(); Map.put ("1", "AA"); Map.put ("2", "BB"); Map.put ("3", "CC"); //Method 1: Use iterators, Fetch keyset ()Iterator iterator=Map.keyset (). iterator ();  while(Iterator.hasnext ()) {String key=Iterator.next (). toString (); String value=Map.get (key); System.out.println ("Key=" +key+ "value=" +value); }        //Method 2: Use iterators, Fetch entryset ()Iterator iterator2=Map.entryset (). iterator ();  while(Iterator2.hasnext ()) {Map.entry<string, string> entry= (map.entry<string,string>) Iterator2.next (); System.out.println (Entry.getkey ()+"  "+Entry.getvalue ()); }

(2) Enhance for loop, rewrite the above 2 methods

        // Method 1         for (String s:map.keyset ()) {            System.out.println (s+ "  " +Map.get (s));        }         // Method 2 Using the Enhanced For,entryset ()         for (Map.entry me:map.entrySet ()) {            System.out.println (Me.getkey () + "  " +Me.getvalue ());        }

2

Summarize for loop and for loop enhanced traversal arrays, List,set and map

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.