class Config() {
var items = HashMap<String, Int>()
items.put("D", 1000)
items["A"] = 10
items["B"] = 20
items["C"] = 30
}
I think there is no problem, But the error happend "expecting member declaration" How can I dodge that error
CodePudding user response:
you can't just fill in hashMap in the middle of the class. Do it in init or function
class Config{
private val items = HashMap<String, Int>()
fun putInfo(){
items["A"] = 1000
items["B"] = 500
items["C"] = 200
//...
}
}
or
class Config{
private lateinit var items: HashMap<String, Int>
init {
items = HashMap<String, Int>()
items["A"] = 1000
items["B"] = 500
items["C"] = 200
//...
}
}
CodePudding user response:
class Config {
lateinit var items: HashMap<String, Int>
init {
items.put("D", 1000)
items["A"] = 10
items["B"] = 20
items["C"] = 30
}}