Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, a range is a unique concept that defines a start value, an end value, and the ability to iterate over those values. Kotlin ranges are powerful and allow for expressive operations. Let's dive into them!
The most common way to define a range is using the ..
operator:
val intRange = 1..5 // This defines a range from 1 to 5 (inclusive)
Ranges can be defined for any type that implements the Comparable
interface, but they're most commonly used with numbers.
Ranges are often used in for
loops:
for (i in 1..5) { println(i) // This will print numbers from 1 to 5 }
You can use the in
keyword to check if a value is within a range:
val num = 3 if (num in 1..5) { println("$num is in the range") } else { println("$num is out of the range") }
You can create a range that goes in the opposite direction using the downTo
function:
for (i in 5 downTo 1) { println(i) // This will print numbers from 5 to 1 }
The step
function allows you to specify an interval between values in the range:
for (i in 1..10 step 2) { println(i) // This will print 1, 3, 5, 7, 9 }
If you want to exclude the end value from the range, you can use the until
function:
for (i in 1 until 5) { println(i) // This will print 1, 2, 3, 4 (5 is excluded) }
Ranges aren't limited to numbers. They can be used with characters as well:
for (char in 'a'..'e') { println(char) // This will print a, b, c, d, e }
You can check if a range is empty by using the isEmpty
property:
val r = 5 downTo 10 println(r.isEmpty) // This will print true
Ranges in Kotlin are a versatile tool that can simplify many operations that involve sequences of numbers or characters. By using the built-in functions and properties associated with ranges, you can write more concise and expressive Kotlin code.
Creating ranges in Kotlin:
..
operator.val intRange = 1..10 val charRange = 'a'..'z'
Inclusive vs exclusive ranges in Kotlin:
val inclusiveRange = 1..5 // [1, 2, 3, 4, 5] val exclusiveRange = 1 until 5 // [1, 2, 3, 4]
Using ranges with loops in Kotlin:
for
loop.for (num in 1..5) { // Do something with num }
Checking for range membership in Kotlin:
in
operator to check if a value is within a range.val value = 3 val isInRange = value in 1..5 // true
Filtering collections with ranges in Kotlin:
val numbers = listOf(1, 2, 3, 4, 5) val filteredNumbers = numbers.filter { it in 2..4 } // [2, 3, 4]
Range operators in Kotlin:
downTo
, step
for custom ranges.val descendingRange = 5 downTo 1 // [5, 4, 3, 2, 1] val steppedRange = 1..10 step 2 // [1, 3, 5, 7, 9]
Checking if a value is within a range in Kotlin:
contains()
method to check if a value is within a range.val range = 1..5 val isWithinRange = range.contains(3) // true
Range functions and methods in Kotlin:
rangeTo()
, until()
, and methods like contains()
on ranges.val range = 1.rangeTo(5) // [1, 2, 3, 4, 5] val exclusiveRange = 1.until(5) // [1, 2, 3, 4]
Using ranges with when expressions in Kotlin:
when
expressions for concise pattern matching.val score = 75 when (score) { in 90..100 -> println("A") in 80..89 -> println("B") // ... }
Range pattern matching in Kotlin:
when
expressions.val value = 42 when (value) { in 1..10 -> println("In the range 1-10") in 11..20 -> println("In the range 11-20") else -> println("Outside the specified ranges") }
Working with date ranges in Kotlin:
val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) val dateRange = startDate..endDate
Custom ranges in Kotlin:
iterator
logic.class DateRange(val start: LocalDate, val endInclusive: LocalDate) : Iterable<LocalDate> { override fun iterator(): Iterator<LocalDate> { return DateIterator(start, endInclusive) } }