Arrays of different elements

At the beginning of this section, we discussed the following methods of declaring arrays:

val intArray = arrayOf(1,2,3)
val charArray = arrayOf('a','b','c')

Using these methods, each array can contain values of the same types. Kotlin also allows us to create an array that contains different types of values. The arrayOf function not only takes a list of similar values but also takes a generic type, vararg. This stands for variable arguments, and we can use this type to pass mixed values:

fun funcVararg() {
val array = arrayOf(1,"TWO",'c',4.0)
for (i in array) {
println(i)
}
println(array.contains(2))
println(array.contains('c'))
}

This code prints all the values on the screen and also verifies whether the specific values of 2 and c are in the array.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.204.5