Kotlin’s: function calls, plus (adding elements), copyOf (copying arrays), Reverse (flipping arrays), forEach (iterating arrays), filter (filtering arrays), Map function operations and extensions, Reduce, fold, filter, filterI Explanation and use examples of the ndexed, takeWhile, infix keyword, and so on.

The description is already in the code, look at the code. If you have any questions or suggestions, please leave a comment. thank you

Other uses of functions :: Other ways of manipulating functions.

class ComposeCall {
    fun sayHi() {
        System.out.println("ComposeCall,sayHi:Hi!")
    }

    fun sayHiToAny(any: Any) {
        System.out.println("--->ComposeCall,sayHiToAny:Hi," + any)
    }
}
Copy the code

validation

Var hi1 = ComposeCall: : sayHiToAny / / returns the method itself, rather than return value println ("hil:"+ hi1) / / / / output hil: fun com.tanksu.ndkdemo3.feature.cls.Com poseCall. SayHiToAny (kotlin. Any) : Kotlin. Unit var hi2 = ComposeCall::sayHi var com = ComposeCall() hi2(com)"jack"// ComposeCall,sayHi:Hi! // ComposeCall,sayHi:Hi! // --->ComposeCall,sayHiToAny:Hi,jackCopy the code

The manipulation of arrays by methods such as plus (adding elements), copyOf (copying arrays), reverse (flipping arrays), forEach (iterating arrays), and filter.

        val arr = arrayOf("Apple"."Banana".""."Orange") arr. ForEach (::println)// Empty objects also print println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -")
        var narr = arr.plus("juice"Var newArr = narr.copyof ()// copy a newArr newarr.foreach (::println) println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -") arr.reverse()// Reverse the array arr.foreach (::println) println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -") println(arr.filter(String::isNotEmpty))//filter will filter non-empty objects // Apple // / Banana // // Orange // -------------------------- // Apple // Banana // // Orange // juice // -------------------------- // Orange // // Banana  // Apple // -------------------------- // [Orange, Banana, Apple]Copy the code

List map function operations and extensions to the array for custom operations.

ArrayList<Int>() raw.foreach {newlist.add (it + 1)//it replaces each element in raw, } println(newList) println(raw.map {it * 10})//map iterated through the raw elements, Println (raw.map(Int::toDouble))//map makes each element a double, Then do print / / [2, 3, 4, 5, 6, 6] / [10, 20, 30, 40, 50, 50] / / [1.0, 2.0, 3.0, 4.0, 5.0, 5.0]Copy the code

Array reduce function: iterates through array elements and customizes operations. Similar to fold.

Val list = listOf(2, 3, 5) println(list.reduce {ret, I -> ret + I}) Finally, output the result to ret... // Add println(list.reduce {ret, I -> ret + I}) // output: 10Copy the code

Use the fold function to customize an array. Similar to Reduce.

    val list = listOf(2, 3, 5)
    println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"Fold (raw.fold(10) {ret, I -> ret + I}) println(raw.fold(10) {ret -> ret + I})"jack say:") {ret, I -> ret.append(I)}) In the second parameter to be able to append action / / output / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / / / jack say: 20, 235Copy the code

Array methods such as Filter, filterIndexed, takeWhile manipulate array elements.

Val list = listOf (4, 3, 2, 5, 6, 8, 9) println (list, a filter {it = = 3 | | it = = 6}) / / filter is equal to the set of 3 or equal to 6 elements, The result is a new set that returns println(list.filterIndexed {index, I -> index == I})// filters the set of elements equal to the first argument, the result is a new set that returns println(list.takewhile {itin0.. 5})// Filter elements between 0 and 5, resulting in a new collection println(list.takeWhile {itin3.. 5})// Filter the elements between 3 and 5, the result is returned as a new set. // [3, 6] // [2] // [4, 3, 2, 5]Copy the code

Infix: another way to call a function. (SAO operation)

        class TestInfix(var age: Int) {
            infix fun dog(weight: Int) {
                println("TestInfix, dog $age and $weight")
            }

            infix fun cat(weight: String) {
                println("TestInfix, dog $age and $weight"}} infix fun testinfix.rat (mounth: Int) {// This is println("TestInfix rat $age and $mounth")
        }

        var i = TestInfix(3)
        i dog 80
        i rat 1
        i cat "jack"
//        TestInfix, dog 3 and 80
//        TestInfix rat 3 and 1
//        TestInfix, dog 3 and jack
Copy the code

Nonnull judgment:

To use? The call is a safe call that returns null if the called quantity is empty. Void pointer exception (NPE) is invoked when the called quantity is null, or a condition is used to determine (if-else) to decide in advance what to do with empty spaceCopy the code