Jazim
Jazim

Reputation: 1

How to make an array of arrays in kotlin?

I have an array of userIDs, which contains specific users from a certain group.

{userID1,userID2}

val userIDArrayList: ArrayList<String> = ArrayList()
    userIDArrayList.add(userID1)
    userIDArrayList.add(userID2)

I want to make a master array which contains several different user arrays.

[{userID1, userID2}, {userID3, userID4}]

How can I do that in kotlin?

Upvotes: 0

Views: 829

Answers (2)

Jolan DAUMAS
Jolan DAUMAS

Reputation: 1374

First, declare the master array which will contains other arrays

    val masterList = mutableListOf<List<String>>()

Secondly, declare the arrays which will be nested. Assuming userID1, userID2, userID3, userID4 are declared somewhere

    val subList1 = listOf(userID1,userID2)
    val subList2 = listOf(userID3,userID4)

Finally, add the sublists to the master

    masterList.add(subList1)
    masterList.add(subList2)

Of course you can do all of this with only one line during masterList instantiation

    val masterList = mutableListOf<List<String>>(
        listOf(userID1,userID2),
        listOf(userID3,userID4)
    )

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13358

mutableListOf(mutableListOf("yourobject"),mutableListOf("yourobject"))

or

val myList = mutableListOf<MutableList<yourobject>>()

Upvotes: 1

Related Questions