5
I’m trying to do something that I think is extremely simple: Given a class called Student
, i instated 3 students and added on a list (mutableListOf
). That done, I want to access the name of the students on the list.
It seems simple, doesn’t it? If I were to implement a programming language I would do so:
lista.get(1).name
But in Kotlin it’s not like that, and worse, I still haven’t figured out how!
Below is an example code:
class Student(
name : String,
age : Int,
course : String,
)
fun main() {
var s1 = Student("Ana", 25, "Eng")
var s2 = Student("Joao", 30, "Geo")
var s3 = Student("Flavio", 17, "Publicidade")
var s4 = Student("Bruno", 33, "Adm")
var test = mutableListOf(s1,s2,s3)
test.add(s4)
println(test.get(1))
}
Try adding
val
orvar
before the name of the variables in the constructor. https://kotlinlang.org/docs/reference/classes.html#constructors– Leonardo Lima