🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The arrayOf function in Kotlin is used to create an array of specified elements. This function is part of the Kotlin standard library and provides a simple way to initialize arrays.
Table of Contents
- Introduction
arrayOfMethod Syntax- Examples
- Basic Usage
- Creating an Array of Custom Objects
- Real-World Use Case
- Conclusion
Introduction
The arrayOf function returns an array containing the specified elements. It is a convenient way to create arrays without needing to explicitly declare the size.
arrayOf Function Syntax
The syntax for the arrayOf function is as follows:
fun <T> arrayOf(vararg elements: T): Array<T>
Parameters:
elements: The elements to be included in the array. This parameter is of a variable argument type, allowing you to pass multiple elements.
Returns:
- An array containing the specified elements.
Examples
Basic Usage
To demonstrate the basic usage of arrayOf, we will create an array of integers and print its contents.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
println("Array of numbers: ${numbers.joinToString()}")
}
Output:
Array of numbers: 1, 2, 3, 4, 5
Creating an Array of Custom Objects
This example shows how to use arrayOf to create an array of custom objects.
Example
class Person(val name: String, val age: Int) {
override fun toString(): String {
return "Person(name='$name', age=$age)"
}
}
fun main() {
val people = arrayOf(
Person("Ravi", 25),
Person("Anjali", 30)
)
println("Array of people: ${people.joinToString()}")
}
Output:
Array of people: Person(name='Ravi', age=25), Person(name='Anjali', age=30)
Real-World Use Case
Initializing Data Collections
In real-world applications, arrayOf can be used to initialize collections of data, such as initializing an array of configurations or a list of user inputs for processing.
Example
fun main() {
val configurations = arrayOf("config1", "config2", "config3")
configurations.forEach { config ->
println("Loading configuration: $config")
}
}
Output:
Loading configuration: config1
Loading configuration: config2
Loading configuration: config3
Conclusion
The arrayOf function in Kotlin is a convenient way to create arrays with specified elements. It simplifies the array initialization process and can be used with various data types.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment