📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn about Arrays in Kotlin with lots of examples. We will also see some of the important Kotlin Array operations or methods.
An array is a collection of a fixed number of values. The array items are called elements of the array. Each element can be referred to by an index. Arrays are zero-based.
Example 1: Create and Initialize an Array in Kotlin
import java.util.Arrays
fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
println(Arrays.toString(nums))
val nums2 = (3..12).toList().toTypedArray()
println(Arrays.toString(nums2))
val nums3 = IntArray(5, { i -> i * 2 + 3})
println(Arrays.toString(nums3))
}
[1, 2, 3, 4, 5]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[3, 5, 7, 9, 11]
Example 2: Kotlin Array Basic Operations
import java.util.Arrays
fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
println(nums.get(0))
nums.set(0, 0)
println(Arrays.toString(nums))
val nums2 = nums.plus(1)
println(Arrays.toString(nums2))
val slice = nums.sliceArray(1..3)
println(Arrays.toString(slice))
println(nums.first())
println(nums.last())
println(nums.indexOf(5))
}
1
[0, 2, 3, 4, 5]
[0, 2, 3, 4, 5, 1]
[2, 3, 4]
0
5
4
Example 3: Kotlin Array Traversal or Looping
In this Kotlin example, we will discuss four different ways to loop over an Array in Kotlin.
fun main() {
val nums = arrayOf(1, 2, 3, 4, 5, 6, 7)
nums.forEach({ e -> print("$e ") })
println()
nums.forEachIndexed({i, e -> println("nums[$i] = $e")})
for (e in nums) {
print("$e ")
}
println()
val it: Iterator<Int> = nums.iterator()
while (it.hasNext()) {
val e = it.next()
print("$e ")
}
}
Output:1 2 3 4 5 6 7
nums[0] = 1
nums[1] = 2
nums[2] = 3
nums[3] = 4
nums[4] = 5
nums[5] = 6
nums[6] = 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
fun main() {
val nums = arrayOf(1, 2, 3, 4, 5, 6, 7)
nums.forEach({ e -> print("$e ") })
println()
nums.forEachIndexed({i, e -> println("nums[$i] = $e")})
for (e in nums) {
print("$e ")
}
println()
val it: Iterator<Int> = nums.iterator()
while (it.hasNext()) {
val e = it.next()
print("$e ")
}
}
1 2 3 4 5 6 7
nums[0] = 1
nums[1] = 2
nums[2] = 3
nums[3] = 4
nums[4] = 5
nums[5] = 6
nums[6] = 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
Example 4: Kotlin Array count
fun main() {
val nums = intArrayOf(2, 3, 4, 5, 6, 7)
println("There are ${nums.count()} elements in the array")
val nOfEvens = nums.count { it % 2 == 0 }
println("There are $nOfEvens even values in the array")
}
There are 6 elements in the array
There are 3 even values in the array
Example 5: Sorting Arrays in Kotlin
fun main() {
val nums = arrayOf(7, 3, 3, 4, 5, 9, 1)
val sortedNums = nums.sortedArray()
println(Arrays.toString(sortedNums))
val sortedNumsDesc = nums.sortedArrayDescending()
println(Arrays.toString(sortedNumsDesc))
}
[1, 3, 3, 4, 5, 7, 9]
[9, 7, 5, 4, 3, 3, 1]
Example 6: Kotlin Filtering Arrays
fun main() {
val nums = arrayOf(1, -2, 3, 4, -5, 7)
nums.filter { e -> e > 0 }.forEach { e -> print("$e ") }
}
1 3 4 7
Comments
Post a Comment
Leave Comment