0

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?

1
  • 1
    Just one note, these are Lists not Arrays. It's called ArrayList because it's a list implemented with arrays, but it's not equivalent to an array. Commented Jun 4, 2022 at 9:58

2 Answers 2

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

or

val myList = mutableListOf<MutableList<yourobject>>()
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Can we send the mutable list via an intent to another activity?

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.