- Declaring and initializing an array of integers
val numbers = arrayOf(1, 2, 3, 4, 5)
(or)
val numbers = intArrayOf(1, 2, 3, 4, 5)
print(numbers.contentToString()) //[1,2,3,4,5 ]
- Printing the elements of an array using a for loop
val numbers = arrayOf(1, 2, 3, 4, 5)
for (i in 0 until numbers.size) {
println(numbers[i])
}
// if you have a mixed array of int and string you can use "is" keyword.
// if(i is Int){}
- You can also use the "forEach" function, which takes a lambda as an argument. The lambda is called for each element in the array:
val numbers = arrayOf(1, 2, 3, 4, 5)
numbers.forEach { println(it) }
- Adding and Deleting Element
var numbers = arrayOf(1, 2, 3, 4, 5)
numbers+=8
numbers-=8