🎓 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 size property in Kotlin is used to get the number of elements in an array. This property is part of the Kotlin standard library and provides a simple way to retrieve the length of an array.
Table of Contents
- Introduction
sizeProperty Syntax- Understanding
size - Examples
- Basic Usage
- Using
sizewith Custom Types
- Real-World Use Case
- Conclusion
Introduction
The size property returns the number of elements in an array. It is a straightforward and effective way to determine the length of an array.
size Property Syntax
The syntax for the size property is as follows:
val size: Int
Parameters:
- This property does not take any parameters.
Returns:
- The number of elements in the array.
Understanding size
The size property is used to get the total number of elements in an array. This is particularly useful when iterating over arrays or performing operations that depend on the array's length.
Examples
Basic Usage
To demonstrate the basic usage of size, we will create an array of integers and print its size.
Example
fun main() {
val numbers = arrayOf(10, 20, 30, 40, 50)
println("Size of the array: ${numbers.size}")
}
Output:
Size of the array: 5
Using size with Custom Types
This example shows how to use size to get the number of elements in 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),
Person("Priya", 22)
)
println("Size of the people array: ${people.size}")
}
Output:
Size of the people array: 3
Real-World Use Case
Iterating Over Arrays
In real-world applications, the size property is often used to iterate over arrays, ensuring that the iteration covers all elements.
Example
fun main() {
val tasks = arrayOf("Task 1", "Task 2", "Task 3")
for (i in 0 until tasks.size) {
println("Task at index $i: ${tasks[i]}")
}
}
Output:
Task at index 0: Task 1
Task at index 1: Task 2
Task at index 2: Task 3
Conclusion
The Array.size property in Kotlin is a convenient way to get the number of elements in an array. It is particularly useful for iteration and operations that depend on the array's length. By understanding and using this property, you can effectively manage array operations in your Kotlin applications.
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