Kotlin Map Question

Asked 2 years ago, Updated 2 years ago, 63 views

I'm studying Kotlin on my own, but I don't understand this part, so I'm asking a question


var operPriorityMap: Map<Char, Int> = mapOf(
'(' to 0,
'+' '+' to 1,
'-' '-' to 1,
'*' '*' to 2,
'/' '/' to 2)

var tokenPriority: Int = operPriorityMap.get('+')
var topPriority: Int = operPriorityMap.get('*')

if(tokenPriority > topPriority){
    //
}

The message below appears in the operPriorityMap.get() section.

Type mismatch. Required:Int Found:Int?

I think it's the same Int type, but I don't know why it's not working. I can't even say to Int(), but is the grammar wrong?

kotlin map get

2022-09-20 21:54

2 Answers

Check the nullable part of the coat line.

var tokenPriority: Int? = operPriorityMap.get('+')
var topPriority: Int? = operPriorityMap.get('*')


2022-09-20 21:54

In Kotlin, there is a distinction between a null type and a non-null type.

A ? is nullable.


2022-09-20 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.