Skip to main content
infervour.com

Back to all posts

How to Initialize Object Array In Kotlin?

Published on
3 min read
How to Initialize Object Array In Kotlin? image

Best Kotlin Programming Books to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
+
ONE MORE?

In Kotlin, you can initialize an object array using the arrayOf function. This function allows you to create an array of objects with specified values.

To initialize an object array in Kotlin:

  1. Use the arrayOf function followed by parentheses to create an array object.
  2. Inside the parentheses, provide the values of the objects you want to initialize the array with, separated by commas.

Here's an example of how to initialize an object array with three elements:

val myArray = arrayOf("Object 1", "Object 2", "Object 3")

In this example, myArray is the name of the array variable, and it is assigned an array object containing three elements: "Object 1", "Object 2", and "Object 3".

You can replace the string values with any other types of objects that you want to initialize the array with, as long as they match the element type of the array.

Hope this helps!

What are the steps to initialize an object array in Kotlin?

To initialize an object array in Kotlin, you can follow these steps:

  1. Declare a variable to hold the array. For example:

var myArray: Array

  1. Assign a new array of the desired size to the variable using the Array() constructor. For example, to create an array of size 10:

myArray = Array(10) { MyObject() }

  1. Optionally, you can directly initialize the array with specific objects:

myArray = arrayOf(MyObject(), MyObject(), MyObject())

Here, Array() represents the constructor for creating a new array, and MyObject() represents the constructor of the object to be stored in the array.

Note: Replace MyObject with the actual class name of the objects you want to store in the array.

How to pass an object array as a method parameter in Kotlin?

To pass an object array as a method parameter in Kotlin, you can follow these steps:

  1. Define the object class or data class:

data class MyObject(val name: String, val age: Int)

  1. Create an object array:

val myObjects = arrayOf( MyObject("John", 25), MyObject("Jane", 30), MyObject("Alice", 35) )

  1. Define a method that takes an object array as a parameter:

fun printMyObjects(objects: Array) { for (obj in objects) { println("Name: ${obj.name}, Age: ${obj.age}") } }

  1. Pass the object array to the method as an argument:

printMyObjects(myObjects)

When you run the code, it will iterate through the object array and print the name and age values of each object in the array.

How do I declare an object array in Kotlin?

To declare an array of objects in Kotlin, you can follow these steps:

  1. Define a class for the object type. For example, let's consider a class called Person:

class Person(val name: String, val age: Int)

  1. Declare an array of Person objects using the Array class and its constructor. Specify the size of the array by passing the desired length as the first argument, followed by the initializer function that defines the objects in the array.

val people: Array = Array(5) { Person("John Doe", 25) }

In this example, we've created an array of Person objects with a length of 5. Each object is initialized with the name "John Doe" and age 25.

Alternatively, you can use the arrayOf function to create and initialize the array with the objects directly.

val people: Array = arrayOf( Person("John Doe", 25), Person("Jane Smith", 30), Person("Mark Johnson", 40) )

In this case, we've created an array of Person objects with 3 elements, each initialized with the specified name and age.

That's it! You have successfully declared an object array in Kotlin.