10
[ [Data(name1, good) ,Data(name2,good)] , [Data(name2,good), Data(name2,bad)] ]

How to convert this List into ArrayList please?

3 Answers 3

12
var list = [ [Data(name1, good) ,Data(name2,good)] , [Data(name2,good), Data(name2,bad)] ]

var arraylist = ArrayList(list)

Sign up to request clarification or add additional context in comments.

Comments

1

Well first, that is not how to define a list in Kotlin. Since there are no list literals in Kotlin.

Instead, it's like listOf(1, 2, 3, 4,) for a normal list, and mutableListOf(1, 2, 3, 4,) for a mutable (editable) list.

MutableList is basically an ArrayList in Kotlin. But there is still arrayListOf() in Kotlin.

There is also toMutableList() extension to most Collection subclasses in Kotlin

1 Comment

list.toTypedArray()
0

convert List to ArrayList in Kotlin

fun getAllHome(){
       var list : List<HomeEntity> = ArrayList()
       var myArrayList= list.listToArrayList()
    }

companion object {
    fun <T> List<T>.listToArrayList(): ArrayList<T> {
        val array: ArrayList<T> = ArrayList()
        for (index in this) array.add(index)
        return array
    }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.