🤔 But How it actually works ?
1) First of all let's look at the code that was given to us:
var myArray:Array = arrayOf(1,2,3,4,null)
In this code when you give your attention to the
" ? " it means that the elements of this Type of variable can be null like this
var myNumber:Int? = null // works
var myNumber2:Int = null // this will give us error
With that thing we're telling to our type that that: my element can be null at somewhere and you have to work even though it has a null value and it works properly without any problems and exceptions
2) And now when if we do like this in our code
var a = myArray.map { it * 2 }
This code won't work because of
myArray can have a null value which we talked at first.
And that's why it can't multiply
( a null for * 2) and for fixing that problem we use if the elements has no any null value like this
filterNotNull()
This method tells that : when it has a null value then give me each one of their multiplication but except null elements and it works .