If the array is small and you want to map an action to each index:
You could use indexOfFirst
to determine the smallest index which meats your condition. Then you can use a when
statement to decide what to do.
when(nameArray.indexOfFirst{ it == name }) {
0 -> // do something
1 -> // do something else
//...
else -> // do something different
}
In case you might want to do the same thing for multiple indices you can use comma separated values. In case the indices are consecutive, you can use ranges:
when(nameArray.indexOfFirst{ it == name }) {
0 -> // do something
1, 2 -> // do the same thing for 1 and 2
in 3..6 -> // do the same thing for 3, 4, 5 and 6
//...
else -> // do something different
}
In order to use this syntax it is a good idea to do index retrieval (like shown) first.
If the array is big and you really only want to check for specific elements:
when(name) {
nameArray[0] -> // do something
nameArray[1] -> // do something
nameArray[2] -> // do something
nameArray[3] -> // do something
else -> // other action
}