Lesson 39: Lambda Expression with Array Data Types

Lesson 39: Lambda Expression with Array Data Types

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

We saw in previous concepts that for the basic types; Byte, Short, Int, Long, Float, Double, Char and Boolean we have a series of classes to define arrays of these types: ByArray, ShortArray, IntArray, LongArray, FloatArray, DoubleArray, CharArray and BooleanArray.

We saw how to create for example an array of 3 integers and its subsequent loading with the syntax:

    Var array = IntArray(3)
    array[0] = 10
    array[1] = 10
    array[2] = 10    

The IntArray class has a series of methods that require a lambda expression, as we did not know the lambda expressions we did not use them in the concepts that we saw arrays.

The IntArray class like the others we name (ByteArray, ShortArray, etc.) have a second constructor with two parameters, the first one indicates the number of elements of the array and the second one must be a lambda expression that indicates the value to be stored in each Component of the arrangement:

    Var array = IntArray (10) {5}

An array of 10 elements is created and the number 5 is stored in each component.

If we want to store in the first component a 0, in the second a 1 and so on we can arrange the following syntax:

    Var array = IntArray (10) {it}

This works because the higher order function when calling our lambda expression for each component of the array sends us the position. Remember that in a long way we can express it with the following syntax:

    Var array = IntArray (10) {index -> index}

If we want to save the values 0,2,4,6 etc. We can use the syntax:

    Var array = IntArray (10) {it * 2}

If we want to store random values between 1 and 6 we can create the object of the IntArray class with the following syntax:

    Var array = IntArray (10) {((Math.random () * 6) + 1) .toInt ()}

Problem 1

Create an object of the 20-element IntArray class with random values between 0 and 10.

Print of the arrangement:

  • All the elements.
  • Number of items less than or equal to 5.
  • Display a message if all items are less than or equal to 9.
  • Display a message if at least one of the array elements stores a 10.

Project152 – Principal.kt

fun main (parameter: Array <String>) {
    var array = IntArray (20) {(Math.random () * 11) .toInt ()}
//coding180.com
    println ("Complete array listing")
    for (element in array)
        print ("$element")
    println()
    val count1 = array.count {it <= 5}
    println ("Number of items less than or equal to 5: $count1")
    if (array.all {it <= 9})
        println ("All items are less than or equal to 9")
    else
        println ("Not all elements are less than or equal to 9")
    if (array.any {it == 10})
        println ("At least one of the elements is a 10")
    else
        println ("All elements are different than 10")
}

To solve these algorithms we use a series of methods that provides the class IntArray.

We define an object of the 20-element IntArray class and pass a lambda expression to generate random values between 0 and 10:

Fun main (parameter: Array <String>) {
    Var array = IntArray (20) {(Math.random () * 11) .toInt ()}

Complete Listing of Arrangement:

    Println ("Complete array listing")
    For (element in array)
        Print ("$element")

To count the number of elements equal to the value 5, the IntArray class has a method called count that has a single parameter of type function:

    Val count1 = array.count {it <= 5}

When a higher-order function has a single parameter as it does with count, it is not necessary to have the parentheses:

    Val count1 = array.count () {it <= 5}

The longest and weakest way to encode the lambda expression with a single parameter is:

Val count1 = array.count ({value -> value <= 5})

If we want to analyze if all the elements of the array satisfy a condition we have the function all (returns a Boolean):

    If (array.all {it <= 9})
        Println ("All items are less than or equal to 9")
    Else
        Println ("Not all elements are less than or equal to 9")

To verify if any of the elements meet a condition we can use any method by passing the expression lambda:

    If (array.any {it == 10})
        Println ("At least one of the elements is a 10")
    Else
        Println ("All elements are different than 10")

We should always consult the official website of Kotlin where all classes of the standard library are documented, in our case we should consult the IntArray class.

Proposed problem

  • Create a 10-element FloatArray type array, load its elements by keyboard.
    Print the number of values entered smaller than 50.
    Print a message if all values are less than 50.
Solution

About The Author

Related posts

Leave a Reply