Kotlin series nine - destructuring declarations, collections

First, the introduction of destructuring function

      The learning summary of kotlin has also been delayed for a long time. Today, I will continue to learn destructuring declarations and collections. This article is relatively simple and is mostly an introduction to methods and usage. Let’s start. If we create a class and several properties in Java, the code is as follows:

public class Person {
 private String name;
 private int age;
 private String sex;

    public Person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

Create an object of Person:

Person person = newPerson(“A”,20,“man”);

How to implement the above code in Kotlin?

/ / Declare a data class, use data modification
data class JieGou(var name:String,var age : Int){
}

When creating an object, it is equivalent to declaring two variables and assigning them accordingly
var (name,age) = Run.JieGou("A",20)

Create a data class, and assign values ​​when creating an object. At this time, not only the object of JieGou is created, but also two variables, name and age, are declared. One destructuring declaration creates multiple variables at the same time. , and they can be used independently, such a syntax is called a destructor

Second, the use of destructuring functions

  • When the destructor creates an object, it creates an object for each property at the same time, and can be used directly:
When creating an object, it is equivalent to declaring two variables and assigning them accordingly
var (name,age) = Run.JieGou("A",20)
Log.e("$name-----", "$age")

  • When you only need to use a certain property at a special moment without declaring redundant variables, just replace it with an underscore:
Use _ (underscore) instead when you don't need a value
var (_,age) = Run.JieGou("A",20)
  • Destructuring declarations are also applied in the map collection, so when iterating over the map collection you can:
At this time, the key and value correspond to the key-value pair in the map
for ((key,value) in map){
    Log.e("$key-----", "$value")
}

3. Collection

Collections in kotlin are divided into: mutable collections and immutable collections (lists, sets, maps, etc.)

  1. List : List<out T> type is an interface that provides read-only operations such as size, get, etc. (read-only)

var readList : List<Int> = listOf(1,2)
readList.size
readList.get(index = 0)
  • MutableList<T> can write to changeable collections, operate interfaces such as set (readable, writable)
var writeList : MutableList<Int> = mutableListOf(1,2)
writeList.size
writeList.get(index = 0)
writeList.add(3)

  2. Set: The setOf() type is a type that provides read-only operations such as size (read-only)

var readSet : Set<String> = setOf("A","B")
readSet.size

  • mutableSetOf () can write to changeable collections, operate interfaces such as set (readable, writable)
var writeSet : MutableSet<String> = mutableSetOf("C","D")
writeSet.size
writeSet.add("ER")
  • When an external viewing interface is provided for a writable collection, and the external interface is not allowed to be modified at will, it can be used:
var writeSet : MutableSet<String> = mutableSetOf("C","D")
val read : Set<String>
get() = writeSet.toSet()

3. Other extension methods of list and set:

readList.none() //Return true when the collection is empty
readList.none { it == "B" } // Returns true if the collection has no elements matching this condition
readList.single() // Determine if the collection is a single element and return a single element or an exception
readList.first() returns the first element (when there is only one element, an error will be reported below)
readList.first { it == "B" } returns the first matching element
readList.last() returns the last element
readList.last { it == "B" } The last eligible element cannot be found and returns an exception
readList.findLast { it == "B" } Returns the last element that matches the condition can not find return null
readList.component1() returns the first element
readList.elementAt(0) Find the element out of bounds according to the table and return an exception
readList.elementAtOrElse(6, { "SSS" }) Find the element out of bounds according to the table below and return the default value set
readList.subList(1, 3).toString() Intercept a section of the value of the collection according to the following table

Fourth, the interval

Intervals in Kotlin use .. to indicate

  • (1.. 5) means from 1 to 5, the interval will be automatically superimposed from 1 to 5, you can use step to control the step size of each increase:
for (i in 1..5){
    Log.e("-----", "$i==")  //输出 1,2,3,4,5
}
for (i in 1..5 step 3){
    Log.e("-----", "$i==")  //输出 1,4,
}
  • The interval cannot be automatically reversed. If you want to implement the reverse order function, you need to use downto 
for (i in 5 downTo 1){
    Log.e("-----", "$i==")
}
  • until : The interval modified with until is left closed and opened again, ie [a,b)
for (i in 1 until 5){
    Log.e("-----", "$i==") //Output result: 1,2,3,4
}
  • Range function: rangeTo() produces a data range parameter: the end value of the range
var int: Int = 10
int.rangeTo(20) Get a data range of 10~20 (equivalent to 10..20)
for (i in int.rangeTo(20) step 2){
    Log.e("-----", "$i==")
}
  • downTO() decrements the data from the beginning
  • step() step size for each iteration

That's it for today's study, I hope it can be helpful, get ready to sleep!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325756524&siteId=291194637