The "for" Loop and iteration map in Kotlin

Source: Internet
Author: User

Article Directory
Scene
"For" loop
Iteration Map
Summary Scenario

What is the difference between the "for" loop and the iteration map in Kotlin and the one we know in Java, and what are the better representations? "for" Loop

Kotlin "for" Loop structure: for (I in a.. b
A represents the starting value, and B represents the end value ... Represents the interval. For example (i in 1..100)
Note: The interval is inclusive, which is 1 to 100 (contains 100)

Here's an integer iteration to play the Fizz-buzz game. Note the difference between Java and Kotlin notation

Java package
Javas;
public class Fizzbuzz {
    private static String fizzbuzz (int i) {
        if (i% = = 0) {
            return "Fizzbuzz";
        } else if (i% 5 = = 0) {
            return "Buzz";
        } else if (i% 3 = = 0) {
            return "Fizz";
        } else {
            return "" +i;
        }
    }
    public static void Main (string[] args) {for
        (int i = 1; i < 101; i++) {
            System.out.println (Fizzbuzz (i)); 
  }
    }
}
Kotlin Package
kotlins fun
fizzbuzz (i:int) = when{
        I% = = 0 "fizzbuzz"
        I% 5 = = 0 "Buzz"
        I% 3 = = 0 "Fizz"
        Else, "$i"
} fun

Main (args:array<string>) {
    for (i in 1..100) {//Zone Inter-Iteration
        println (Fizzbuzz (i))
    }
}
Iteration Map

Kotlin Iteration Map Structure: for ((A, b) in map)
A for the map key,b represents the map of Value,a and B is a variable, you can name it
.. Syntax can be used to create a character range. For example, C in ' A ' ... ' F ')

//java package javas; import java.util.Iterator; import Java.util.TreeMap;
    public class Iteratingovermaps {private static character[] chars = new character[]{' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
    private static Treemap<character, string> binaryreqs = new Treemap<character, string> (); public static void Main (string[] args) {for (int i = 0; i < chars.length; i++) {String s = integer.t
            Obinarystring (Chars[i]);
        Binaryreqs.put (Chars[i], s);
        } iterator<character> Iterator = Binaryreqs.keyset (). Iterator ();
            while (Iterator.hasnext ()) {Character key = Iterator.next ();
            String s = binaryreqs.get (key);
        SYSTEM.OUT.PRINTLN (key + "=" + s); }
    }
}
Kotlin Package
kotlins

import java.util.* fun

Main (args:array<string>) {
    val binaryreps = Treemap<char, string> () for
    (c in ' A ' ... ' F ') {//create character interval
        val binary = integer.tobinarystring (C.toint ())  //Convert ASCII code to binary
        binaryreps[c] = binary//According to  Key is c to save binary to map
    } for
    ((letter, binary) in binaryreps) {//iteration map, assign key and value to variable
        println ("$letter = $binary ")
    }
}
Summary

Discover the advantages of Kotlin by comparison:
Use a wider range of (.. Syntax to create a character range)
The code is more concise and capable

If you have a little help, please click to praise, your encouragement is my motivation to write. Thank you.
Welcome to Long Click on the image to identify the QR code or sweep a follow my public number:

My csdn:http://blog.csdn.net/shenshizhong.
My book of Jane: HTTP://WWW.JIANSHU.COM/U/345DAF0211AD

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.