Lesson 36: Arrays with Objects in kotlin

Lesson 36: Arrays with Objects in kotlin

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 said that an array is a data structure that allows storing a set of data of the same type. With a unique name an arrangement is defined and by means of a subscript we make reference to each element of the same (component)

We saw how to create fixes with components of type Int, Char, Float, Double etc., now we will see how to define an array with components of a certain class that we declare ourselves.

Problem 1

Declare a Person class with the properties name and age, define its methods as impression and another that returns true if it is of age or false otherwise
In the main function, define an arrangement with four elements of type Person. Calculate how many people are of age.

Project144 – Principal.kt

class Person (val name: String, val age: Int) {
    fun print () {
        println ("Name: $name Age: $age")
    }

    fun Major () = if (age >= 18) true else false
}

fun main (parameter: Array <String>) {
    val people: Array <Person> = arrayOf (Person ("ana", 22), Person ("juan", 13), Person ("carlos", 6), Person ("maria", 72))
    println ("List of persons")
    for (per in people)
        per.print ()
    var count = 0
    for (per in people)
        if (per.Major ())
            count
    println ("Number of people of age: $count")
}

The Person class declaration defines 2 properties in the same constructor and its two methods:

Class Person (val name: String, val age: Int) {
    Fun print () {
        Println ("Name: $name Age: $age")
    }

    Fun Major () = if (age>= 18) true else false
}

In the main function, we define a variable called people which is an Array with components of type Person. To define its components, we use the arrayOf function provided by Kotlin’s standard library:

    Val people: Array <Person> = arrayOf (Person ("ana", 22), Person ("juan", 13), Person ("carlos", 6), Person ("maria", 72))

The arrayOf function is passed to each of the Person objects.

An Array once created can not resize it.

The easiest way to traverse the Array is through a for:

    For (per in people)
        Per.print ()

In each cycle of the for in the variable per  is stored one of the components of the array.

Similarly to count the number of older people, we proceed to define a counter and through a for we go through the arrangement and call the Major method for each object:

    Var count = 0
    For (per in people)
        If (per.Major ())
            count
    Println ("Number of people of age: $cant")

Acotaciones

To an Array we can access it by means of a subscript or by means of method calls, we can change the value stored in a component etc .:

    // print the data of the person stored in component 0
    People[0].printing ()
    // print the number of components of the array
    Println(personas.size)
    // print the age of the person stored in the last component
    Println (people[3].name)
    // Copy the stored person in the first component in the second
    People [1] = people [0]
    People [0] .printing ()
    People [1] .print ()
    // Access the first component by means of a method instead of a subscript
    People.get (0) .print ()
    // Copy the first component in the third by a method instead of a subscript
    People.set (2, people [0])
    Println ("-------------------------------")
    For (per in people)
        Perprint ()

Problems proposed

  • You have the declaration of the following data class:
    Data class Article (val code: Int, val descripcion: String, var price: Float)
    

    Define an Array with 4 elements of type Article.
    Implement two functions, one that we send the Array and show us all its components, and another that also receives the Array of articles and proceed to increase by 10% all products.

  • Declare a class Given its property value and two methods that allow you to roll the die and print its value.
    In the main define an Array with 5 objects of type Dado.
    Draw the 5 dice and print the values of each one.
Solution


About The Author

Related posts

Leave a Reply