Introduction
In this part, we are going to learn about Kotlin Ranges. Those who are new to Kotlin and willing to learn can go through this article series starting here: Introduction to Kotlin.
Kotlin Ranges
Kotlin range is defined from the start value to the end value. The range is created with the ". ." operator. The double dots define the value which comes inside the defined range.
Example
- val aToZ = 'a'..'z'
-
- val oneToNine = 1..9
Integral Type Ranges
Integral type ranges are the ability to use the for loop. The compiler is used to convert this integral type range.
Example
- fun main(args: Array<String>) {
-
- for (a in 1..5){
-
- print(a )
-
- }
-
- println()
-
- for(x in 'a'..'f'){
-
- print(x )
-
- }
-
- println()
-
- val range = 1.0..5.0
-
- println(range)
-
- println("3.14 in range is ${3.14 in range}")
-
- }
Output
12345
abcdef
1.0..5.0
3.14 in the range is true.
Until Range
The until() keyword is used to exclude the last element. It ranges from start to 1 less than an end.
Example
- for (a in 1 until 6){
-
- print(a )
-
- }
Output
12345
Kotlin range of integer
Kotlin range of integer range using downTo(), downTo, and rangeTo().
Example
- fun main(args: Array<String>) {
-
- for (x in 1..6)
-
- print(x)
-
- println()
-
- for (x in 6 downTo 1)
-
- print(x)
-
- println()
-
- for (x in 1.rangeTo(6))
-
- print(x)
-
- println()
-
- for (x in 6.downTo(1))
-
- print(x)
-
- println()
-
- }
Output
123456
654321
123456
654321
Kotlin range of characters
Kotlin range of character using char data types.
Example
- fun main(args: Array<String>) {
-
- (x in 'a'..'d')
-
- print("$x ")
-
- println()
-
- for (x in 'd' downTo 'a')
-
- print("$x ")
-
- }
Output
abcd
dcba
Kotlin range step
Kotlin range step is used to iterate the range in the interval of an int value.
Example
- fun main(args: Array<String>) {
-
- for (x in 1..12 step 2)
-
- print("$x ")
-
- println()
-
- for (x in 12 downTo 1 step 3)
-
- print("$x ")
-
- }
Output
1 3 5 7 9 11
12 9 6 3
Kotlin range iterator
An iterator() method is used to iterate the range value. It uses the Next() method to check the next element in the range and next() method returns the next element of the range.
Example
- fun main(args: Array<String>) {
-
- val chars = ('a'..'d')
-
- val it = chars.iterator()
-
- while (it.hasNext()) {
-
- val x = it.next()
-
- print("$x ")
-
- }
-
- }
Output
a b c d
Kotlin Utility Functions
Kotlin utility functions have several standard library functions to use in Kotlin Ranges.
The list of utility functions given below:
rangeTo()
downTo()
reversed()
step()
Kotlin rangeTo()
The rangeTo() is used to return the value from start to end in increasing order method. The rangeTo() is integral types which calls the constructors of class.
Example
- fun main(args: Array<String>) {
-
- var range: IntRange = 1.rangeTo(6)
-
- println("Printing value: 1.rangeTo(6)")
-
- for (x in range){
-
- print("$x ")
-
- }
-
- println("")
-
- var range2: IntRange = IntRange(1,6)
-
- println("Printing value: IntRange(1,6)")
-
- for (x in range2){
-
- print("$x ")
-
- }
-
- }
Output
Printing value: 1.rangeTo(6)
1 2 3 4 5 6
Printing value: IntRange(1,6)
1 2 3 4 5 6
Kotlin downTo()
The downTo() is used to return the value in decreasing order method. The downTo() is defined for a pair of integral types.
Example
- fun main(args: Array<String>) {
-
- println("Range 1")
-
- var range1 = 6 downTo 1
-
- for (x in range1){
-
- print("$x ")
-
- }
-
- println()
-
- println("Range 2")
-
- var range2: IntProgression = 6.downTo(1)
-
- for (x in range2){
-
- print("$x ")
-
- }
-
- println()
-
- println("Range 3")
-
- var range3: IntProgression = IntProgression.fromClosedRange(6,1,-1)
-
- for (x in range3){
-
- print("$x ")
-
- }
-
- }
Output
Range 1
6 5 4 3 2 1
Range 2
6 5 4 3 2 1
Range 3
6 5 4 3 2 1
Kotlin reversed()
The reversed() is used to return the reversed order of the given range type.
Example
- fun main(args: Array<String>) {
-
- println("Reversed 1")
-
- var range1 = 1..6
-
- for (x in range1.reversed()){
-
- print("$x ")
-
- }
-
- println()
-
- println("Reversed 2")
-
- var range2: IntRange = IntRange(1,6)
-
- for (x in range2.reversed()){
-
- print("$x ")
-
- }
-
- println()
-
- println("Reversed 3")
-
- var range3 = IntProgression.fromClosedRange(6,1,-1)
-
- for (x in range3.reversed()){
-
- print("$x ")
-
- }
-
- println()
-
- println("Reversed 4")
-
- var range4: IntProgression = IntProgression.fromClosedRange(6,1,-2)
-
- for (x in range4.reversed()){
-
- print("$x ")
-
- }
-
- }
Output
Reversed 1
6 5 4 3 2 1
Reversed 2
6 5 4 3 2 1
Reversed 3
1 2 3 4 5 6
Reversed 4
1 3 5
Kotlin step()
The step() is used to return the range value in the interval of given step value. A step value always takes a positive parameter. The negative step value generates the Illegal Argument Exception.
Example
- fun main(args: Array<String>) {
-
- val range: IntRange = 1..10
-
- println("Print range value with step 2:")
-
- for(x in range step (2)){
-
- print("$x ")
-
- }
-
- println("")
-
- println("Print range value with step 3:")
-
- for(x in range step 3){
-
- print("$x ")
-
- }
-
- val first=((range step 2).first)
-
- val last=((range step 2).last)
-
- println("")
-
- println("First value of interval: $first")
-
- println("Last value of interval: $last ")
-
- }
Output
Print range value with step 2:
1 3 5 7 9
Print range value with step 3:
1 4 7 10
First value of interval: 1
Last value of interval: 9
Conclusion
Kotlin is quickly becoming a complete modern programming language. In this article, we learned about Ranges in Kotlin. In the next part of this series, we will learn about Java Interoperability and Regex. Stay Tuned!