Is it possible to refer to a variable by taking its name from two elements?
for example:
val a1 = 10
val a2 = 5
val n = 1
//val b = an
The short answer is NO. If you want to do look up dynamically, you will have to make your own structure like a Map.
What you ask looks like “dynamic evaluation” in scripting langage. The closest Kotlin features are reflection API and property references.
1 Like
gidds answer is what you want. The reason being is that you are asking for a structure to hold your values. And you want to look up those values based on a name or number.
Gidds’ answer already gives you list
(I would ignore arrays for now) but make sure you learn about map
as well.
2 Likes