Arrays in Kotlin

 package com.example.my_kotlin_practice


fun main(args: Array<String>)
{
val fruits = arrayOf("Apple","Mango","Banana","Orange")
val fruits1 = arrayOf<String>("Apple","Mango","Banana","Orange")
val num = intArrayOf(1,2,3,4)
println(num.contentToString())
println(fruits.contentToString())
println(fruits1.contentToString())
println(fruits[0])
println(fruits1[2])
println(fruits1.get(0))
println(fruits1.get(2))
fruits1.set(
2,"Guava")
println(fruits1.get(2))

println("Size of the fruits1 array is : " + fruits1.size)

for(item in fruits1) {
println(item)
}
if("Mango" in fruits1) {
println("Mango exists in fruits1 array")
}
else {
println("Mango doesn't exists in fruits1 array")
}
println("Below is the usage of distinct() member function :")
val fruits2 = arrayOf<String>("Apple","Mango","Banana","Orange","Apple")
for(item in fruits2.distinct()) {
println(item)
}
println("Below is the usage of drop() member function :")
for(item in fruits2.drop(2)) {
println(item)
}
println("Below is the usage of dropLast() member function :")
for(item in fruits2.dropLast(2)) {
println(item)
}

val fruits3 = arrayOf<String>()
println("Array is empty : " + fruits3.isEmpty())

val fruits4 = charArrayOf('a','b','c','d')
println("Character array is : ${fruits4.contentToString()}")

val fruits5 = doubleArrayOf(1.1,10.10,20.5,45.21)
println("Array of type double is : ${fruits5.contentToString()}")

val array = Array(5) { i -> i }
val iterator = array.iterator()
// println("Below output is performed using 'while' loop :")
// println("The elements in the array are :")
// while(iterator.hasNext()) {
// println(iterator.next())
// }
println("Below output is performed using 'for' loop :")
for(element in iterator) {
println(element)
}
val iterator2 = fruits1.iterator()
println("The elements in the array 'fruits2' is :")
// while(iterator2.hasNext()) {
// println(iterator2.next())
// }
println("Below output is performed using 'for' loop :")
for(element in iterator2) {
println(element)
}

val fruit = Array<String>(4) { "" }
fruit[0] = "Apple"
fruit[1] = "Banana"
fruit[2] = "Orange"
fruit[3] = "Pineapple"
println(fruit.contentToString())

val fruit1 = Array<String>(3) { index ->
when (index) {
0 -> "Android"
1 -> "iOS"
2 -> "Flutter"
else -> "Nothing"
}
}
println(fruit1.contentToString())
val number = Array<Int>(5) { index -> index + 1 }
println(number.contentToString())

val number1 = intArrayOf(10,20,30,40,50)
println(number1.contentToString())
val number2 = doubleArrayOf(1.1,10.10,20.5,22.45)
println(number2.contentToString())
val charValue = charArrayOf('a','b','c','d','e')
println(charValue.contentToString())

val name = arrayOfNulls<String>(4)
name[
0] = "ABC"
name[1] = "Prakash"
name[2] = "Ganesh"
name[3] = "Elephant"
println(name.contentToString())

// val name1 = Array(3) {
// println("Enter name : ")
// readln()
// }
// println(name1.contentToString())

var fd = emptyArray<String>()
fd +=
"Android"
fd += "PHP"
fd += "Flutter"
fd += "Kotlin"
fd += "Java"
println(fd.contentToString())

val course : Array<String> = arrayOf("Android","Flutter","IOS","Kotlin","Dart","Java")
course.
forEach{ println(it) }

val matrix = arrayOf(
arrayOf(10,20,30,40),
arrayOf(50,60,70,80),
arrayOf(90,100,110,120),
arrayOf(130,140,150,160)
)
println(matrix[0][0])
println(matrix[1][2])
println(matrix[2][3])
for(row in matrix) {
for(value in row) {
print(value)
}
println()
}

val array1 = Array(5) { init -> 1 }
val len = array1.size
println("The number of elements in the array is : $len")
println("The elements in the array are : ")
for(i in 0 until len) {
println(array[i])
}

val array2 = Array(5) { index -> index * 2 }
println("Array contents : ${array2.joinToString()}")

val array3 = Array(10) { i -> i }
val len2 = array3.size
println("The number of elements in the array is : $len2")
println("The elements in the array are : ")
for(i in 0 until len2) {
println(array3[i])
}

val array4 = Array(2) { init -> "Sample String" }
val len3 = array4.size
println("The number of elements in the array is : $len3")
println("The elements in the array are : ")
for(i in 0 until len3) {
println(array4[i])
}

val number3 = Array(5) { index -> index + 1 }
println(number3.contentToString())


}

OUTPUT:

[1, 2, 3, 4] [Apple, Mango, Banana, Orange] [Apple, Mango, Banana, Orange] Apple Banana Apple Banana Guava Size of the fruits1 array is : 4 Apple Mango Guava Orange Mango exists in fruits1 array Below is the usage of distinct() member function : Apple Mango Banana Orange Below is the usage of drop() member function : Banana Orange Apple Below is the usage of dropLast() member function : Apple Mango Banana Array is empty : true Character array is : [a, b, c, d] Array of type double is : [1.1, 10.1, 20.5, 45.21] Below output is performed using 'for' loop : 0 1 2 3 4 The elements in the array 'fruits2' is : Below output is performed using 'for' loop : Apple Mango Guava Orange [Apple, Banana, Orange, Pineapple] [Android, iOS, Flutter] [1, 2, 3, 4, 5] [10, 20, 30, 40, 50] [1.1, 10.1, 20.5, 22.45] [a, b, c, d, e] [ABC, Prakash, Ganesh, Elephant] [Android, PHP, Flutter, Kotlin, Java] Android Flutter IOS Kotlin Dart Java 10 70 120 10203040 50607080 90100110120 130140150160 The number of elements in the array is : 5 The elements in the array are : 0 1 2 3 4 Array contents : 0, 2, 4, 6, 8 The number of elements in the array is : 10 The elements in the array are : 0 1 2 3 4 5 6 7 8 9 The number of elements in the array is : 2 The elements in the array are : Sample String Sample String [1, 2, 3, 4, 5] Process finished with exit code 0

Comments