

However, the error message is different this time: “ Kotlin: Val cannot be reassigned.” Next, let’s test the += operator: val myList = listOf("Tom Hanks", "Brad Pitt")Īs we’ve expected, the code doesn’t compile either. If we compile the code above, the compiler complains: “Kotlin: Unresolved reference: add.” This is expected, as the List interface doesn’t have the add() method. But let’s create a test to verify if it’s true: var myList = listOf("Tom Hanks", "Brad Pitt") Since List is read-only, we can’t add an element to it. Implement fun asteroidCollision(asteroids: IntArray): IntArray else if (Math.abs(prev) > Math.Finally, let’s look at the List type in Kotlin.


Smaller ASTs explode (are deleted), the same size both explode, and same direction ASTs do nothingĮxplanation: The 10 and -5 collide resulting in 10.Negative numbers move left, positive numbers move right.Is this a performant strategy to implement a stack in Kotlin to compare and delete values? ExpectĬreate a stack in order to compare an array of asteroids (ASTs) and handle collisions (deletions), returning the final array of ASTs after collisions.
