sooyeon
sooyeon

Reputation: 529

How to Convert List into ArrayList in Kotlin

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

How to convert this List into ArrayList please?

Upvotes: 10

Views: 10916

Answers (3)

Ali Moradi
Ali Moradi

Reputation: 1

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
    }
 }

Upvotes: 0

Ahmad Sattout
Ahmad Sattout

Reputation: 2476

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

Upvotes: 1

Shahriar Nasim Nafi
Shahriar Nasim Nafi

Reputation: 1356

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

var arraylist = ArrayList(list)

Upvotes: 12

Related Questions