No More Hello World
No More Hello World

Reputation: 435

Array inizialization with different data types

I was playing with kotlin features, specially, the inference of data types in a array. All the examples that I saw were like:

val wordArray = arrayOf("empowered", "leveraged", "aligned", "targeted")
val intArray = arrayOf(1, 2, 3, 4)

But it is also possible something like:

val myObjectArray = arrayOf(2, "Dos", 2.2F)

So, you can delcare an array of elements, where each element has a different type. You can work with those items:

val newPhrase = "myObjectArray[0] = ${myObjectArray[0]}, " +
            "myObjectArray[1] = ${myObjectArray[1]}, " +
            "myObjectArray[2] = ${myObjectArray[2]}"
println(newPhrase)
for(item in myObjectArray){
     println(item.javaClass.toString())
}

Which produces in the output console:

myObjectArray[0] = 2, myObjectArray[1] = Dos, myObjectArray[2] = 2.2 
class java.lang.Integer 
class java.lang.String 
class java.lang.Float

I've also checked some silly stuff like:

myObjectArray[0] = 4.4       // It doesn't compile
myObjectArray[1] = 'c'       // It doesn't compile
myObjectArray[1] = 4         // It compiles
myObjectArray[1] = 4.4      // It doesn't compile
myObjectArray[2] = 4        // It compiles
myObjectArray[2] = "cadena" // It compiles   
myObjectArray[2] = 4L       // It doesn't compile

And the output of the compiled lines is:

myObjectArray[0] = 2, myObjectArray[1] = 4, myObjectArray[2] = cadena
class java.lang.Integer
class java.lang.Integer
class java.lang.String

If you want to limit to a concrete data type, lets say bytes, it must be declared like:

var byteArray1 = byteArrayOf(1,2,3)
var byteArray2:Array<Byte> = arrayOf(1,2,3)

Going to the point, my questions are:

  1. Why is it possible to declare an array with different types of items?
  2. What is the adventage?
  3. If there is any adventage, why isn't it wide explained? I read references like kotlin documentation, First Head Kotlin, and some Kotlin blogs. None of them makes any reference about this code option.

I appreciate your clarifications. Thanks in advance.

EDIT:

As @Tenfour04 says, the compiler compiles the code. So the problem is in the IntelliSense of IntelliJ IDEA:

enter image description here

So, it is possible create an array with any type of object, and set its position with any other object.

Upvotes: 0

Views: 525

Answers (2)

Tenfour04
Tenfour04

Reputation: 93571

You might want to check your code again. All of these different types of objects can be added to the Array just fine.

When you use arrayOf without specifying the type, it infers the common type of all the objects you put in. The only common type for Ints, Strings, and Floats is Any, so the resulting array is an Array<Any>. This is just the basic behavior of type inference, so your three questions don't really apply. The use case for an Array<Any> might be rare, but it exists and there's no reason for the compiler to forbid it.

Upvotes: 2

Madina Saidova
Madina Saidova

Reputation: 176

If you want to use an array with different type, you are using polymorphism concept. All classes are subclass of Any (like Object in Java). That is why, what you have done worked. It is useful when you need to store different subclass in one array. For example, array of Instrument can contain Violin, Guitar and etc. The disadvantage of is class instance checking and class casting.

Upvotes: 1

Related Questions