Home > Software engineering >  How to make an array of arrays in kotlin?
How to make an array of arrays in kotlin?

Time:06-04

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?

CodePudding user response:

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

or

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

CodePudding user response:

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)
    )
  • Related