fun main() {
val arr = arrayOf("hello",3,true)
for(item in arr) if(item is Int) println(item + 20 )
// print(arr[1] + 20)
}
I created an array of String, Int and Boolean.
I can do the summation in the for-loop. However, if I am trying to do the print(arr[1] + 20) there is an "Unresolved reference" problem.
Can someone explain this to me? Thanks.
Array<Any>(that just happened to initially contain a String, an Int, and a Boolean). When you pull out a value, all the compiler knows is that it's anAny— unless you check it yourself, as you do within the loop. But that doesn't tell you anything about it outside the loop.