Kotlin in the Set, Map, List containers

Copyright:'ll flowering tree https://blog.csdn.net/qiaoshi96_bk/article/details/83957452

Foreword

Speaking before the Kotlin some basic usage of basic data types and String string concatenation learn here about kotlin container in arrays and collections, in fact, quite want to write up a page with kotlin, after all, so there is a sense of accomplishment, not boring, but the study came from java important know basis, they still step by step.

Array Declarations

kotlin basic types of array declaration, initialize an array of integers:

  var int_arr: IntArray = intArrayOf(1, 2, 3, 4, 5,6 )

It can be seen relative to java, kotlin:

Keywords kotlin declare integer array is: IntArray
kotlin assign a constant array method by intArrayof

From this it can be seen as a way kotlin declare an array: var array name: IntArray (array type) = intArrayof (...)
From this we can see that integer array declaration form kotlin array. But kotlin data that does not contain an array of strings if you want to declare an array of strings:

 var str :Array<String> = arrayOf("hello","world")

See the string may be declared, and that other basic types Can it be so?

   var my_int : Array<Int> = arrayOf(1,2,3)

The answer is yes, and here much like ArrayList in java

Array traversal

We get to know an array of java in length by ".length", traversing an array can for recycling, while the length of the array get through ".size" method in kotlin in. Then take a look at how kotlin is traversed:

      var test_name: Array<String> = arrayOf("小米", "小马", "小张")
        my_name.setOnClickListener {
            var str: String = ""
            var i: Int = 0
            while (i < test_name.size) {
                str = str + test_name[i] + "----"
                i++
            }
            my_name.text = str
        }

Here it is a fact and to Java for loop through the while loop is very similar to my_name assignment.
Here Insert Picture Description
Of course kotlin in the array as well as set and get methods if you want to take a value in the array or set a value only

   test_name.set(2, "小李子")//设置数组中某个位置的值
   str = test_name.get(2)//获取数组中某个位置的值

String segmentation splicing

kotlin divided splicing operation and substantially the same java, substitutions: Replace lookup taken ** indexOf ** ** substring **

   var list:List<String> = str.split(",")

Different places is probably the split in kotlin return value is a "List <String" while in Java is an array of other places you need the actual development utilized.

kotlin in the container

In fact, here is the Java container in the collection, we know there is java Set, List and Map, Kotlin is compatible java in these containers, in kotlin that in these three vessels is how to do?
First and Java each container is divided into different kotlin: Read only two kinds of container and container variable, only the variable container can be CRUD operations.
Since they are container they also have many common API:

  • isEmpty: determining whether the container is empty
  • isNotEmpty: determining whether the non-empty container
  • clear empty the container
  • determining whether the container contains an element comprising
  • iterator iterator
  • obtaining count length of the vessel

The above-mentioned variable kotlin vessel can be divided into read-only container and the container thereof keywords are (left to right are the container type, container name, the container initialization method):

Read-only collection Set setOf
variable collection MutableSet mutableSetOf
read-only queue List listOf
variable queue MutableList mutableListOf
read-only maps Map mapOf
variable mapping MutableMap mutableMapOf

set

Look at several ways to traverse the set kotlin collection:

for in

       //for in 循环
        var food:String = ""
        val food_set: Set<String> = setOf("大米","小米","黑米","糯米")
        for (item in food_set){
         food = food+item
        }
        Log.e("TAG",food)

Iterator iterator

        //迭代器
       val  iterator=food_set.iterator()
        while (iterator.hasNext()){
            val item=iterator.next()
            food = food+item
        }
            Log.e("TAG",food+"---")

forEach iterate

         food = ""
        food_set.forEach { food= food+it }

Here you can see a very clear set of operations actually kotlin traversal and Java are the same.

queue

Traversal queue is basically above the set is the same, we see what action it has unique:
like get, add, removeAt and java same. Key operating MutableList look sort of.

sortBy: ascending order according to the specified conditions
sortByDescending: descending order according to the specified conditions

Usage is as follows

    //队列排序
        var my_list :MutableList<String> = mutableListOf("北京","上海","南京","山东")
        my_list.sortBy { my_list.count() }//升序
        my_list.sortByDescending { my_list.size }//降序

Here Sort determined according to the length of the set, of course, this does not write any practical significance.

Mapping (Map)

Map collection initialization time, there are two ways one is through to the keyword is a Pair

         //to 声明方式
        var my_map: HashMap<Int, String> = hashMapOf(1 to "one", 2 to "two", 3 to "three")
        //Pair 声明方式
        var my_pmap: HashMap<Int, String> = hashMapOf(Pair(1, "one"), Pair(2, "two"), Pair(3, "three"))

Three ways to traverse it:

      // for in 
        var desc = ""
        for (item in my_map) {
            desc = desc + item.key.toString() + "----" + item.value
        }
        Log.e("TAG", desc)

        // 迭代器
        val it = my_pmap.iterator()
        while (it.hasNext()) {
            val item = it.next()
            desc = desc + item.key + "=====" + item.value
        }

        //forEach 遍历
        my_pmap.forEach { key, vaule -> desc = desc + vaule + key }

Not difficult to see the basic usage is relatively simple, relatively easy to use.

Guess you like

Origin blog.csdn.net/qiaoshi96_bk/article/details/83957452