luchito
luchito

Reputation: 125

MutableList<object> variable in method (Kotlin)

is there any way to generate an array of multiple objects?

i want something like this:

doSomething(MutableList<String>) or doSomething(MutableList<Int>) 
fun doSomething(list : MutableList<VariableObject>){}

Upvotes: 0

Views: 45

Answers (2)

Sasi Kumar
Sasi Kumar

Reputation: 13358

Use Any

 fun doSomething(list: MutableList<Any>) {
    val value = list[0]
    when (value) {
        is String -> {
        }
        is Int ->{}
        is yourcustomclass->{}
    }
}

Upvotes: 0

Francesc
Francesc

Reputation: 29330

That's what generics are for,

fun <T> doSomething(list: MutableList<T>) {
}

Upvotes: 3

Related Questions