Press enter to see results or esc to cancel.

Lesson 36: Arrays with Objects in kotlin

Share this

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 ())
    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 ())
    Println ("Number of people of age: $cant")


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
    // 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.

[sociallocker id="958"]
data class Article (val code: Int, val description: String, var price: Float)

fun print (articles: Array <Article>) {
    for (article in articles)
        println ("Code: ${article.code} - Description ${article.description} Price: ${article.price}")

fun increasePrice (articles: Array <Article>) {
    for (article in articles)
        article.price = article.price + (article.price * 0.10f)

fun main (parameter: Array <String>) {
    val articles: Array <Article> = arrayOf(Article (1, "potatoes", 7.5f),
    Article (2, "apples", 23.2f),
    Article (1, "oranges", 12f),
    Article (1, "onion", 21f))

    println ("Current price list")
    print (articles)
    println ("Listing of prices with increase of 10%")
    print (articles)
class Given (var value: Int = 1) {

    fun throww () {
        value = ((Math.random () * 6) + 1) .toInt ()

    fun print () {
        println ("Value of data: $value")

fun main (parameter: Array <String>) {
    var datas: Array <Data> = arrayOf (Given(), Given(), Given(), Given(), Given())
    for (data in datas)

    for (data in datas)
        data.print ()


How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this