Simple self-diary traversal cycle Kotlin

  • Loop
  • Range

Loop key learning here is that for loop, you can iterate through a variety of data for loop, such as Data Map, the data in the List of the data array. Map which is divided into Treemap, hashmap, etc., where do some simple list.

1. 1 to 100 and outputs traversal [1100]

fun main(args:Array<String>)
{
  var num=1..100
  for(num1 in num)
  {
  	println(num1)
  }
}

2. traversed 1-100 and outputs [1,100)

fun main(args:Array<String>)
{
	var num=1 until 100
	  for(num1 in num)
  {
  	println(num1)
  }
}

3. 1 to 100 and outputs the traverse reverse output

fun main(args:Array<String>)
{
	var num=1 until 100
	  for(num1 in num.reversed())
  {
  	println(num1)
  }
}

4. Loop 1 to 100 and outputs a corresponding output position, and

fun main(args:Array<String>)
{
	var num=1 until 100
	  for(num1 in num.withIndex())
  {
  	println(num1)
  }
}

5. traverse List

fun main(args:Array<String>)
{
	var List=listof("土豆","西红柿","牛腩","咖喱") 
	  for(ingredients in num.withIndex())
  {
  	println(ingredients)
  }
}

** 5. Traversing Treemap **

fun main(args:Array<String>)
{
    var map= TreeMap<String,String>()
    map["猫"]="cat"
    map["狗"]="dog"
    map["猴子"]="monkey"
    for((map1,map2) in map)
    {
        println("key是$map1,value是$map2")

    }
}

We need to note the order is not the result of our input to fetch.
TreeMapHere Insert Picture Description

key是狗,value是dog
key是猫,value是cat
key是猴子,value是monkey

** 6. Traversing Array **

val array = arrayOf("a", "b", "c")
        for (element in array){
            println("element=$element")       
             }

Published 15 original articles · won praise 3 · Views 348

Guess you like

Origin blog.csdn.net/Py_csdn_/article/details/104527984