biomusor
biomusor

Reputation: 17

How to declare mutableListOf of arrays?

I want to declare mutableListOf arrays but I don't know how. Google shows me examples like var mutableList1 = mutableListOf<Int>() and etc, but not arrays case(((

import java.util.*

fun main(args: Array<String>) {
    var mutableList1 = mutableListOf<Arrays>()
    var mutableList2 = mutableListOf(arrayOf<Int>())    //works, but it contains empty array:(
    mutableList1.add(arrayOf(1,1))                      //error
}

Upvotes: 0

Views: 288

Answers (2)

You don't need to use mutableListOf for example

val distributionList = mutableListOf<ParticipantDTO>()
participantVolumeList.forEach {
    distributionList.add(
        ParticipantDTO(
            participantUuid = it.get(PARTICIPANT_VOLUMES.PARTICIPANT_UUID)
        )
    )
}

better rewrite to

val distributionList = participantVolumeList.map { mapToParticipant(it) }

private fun mapToParticipant(
    participantVolumesRec: JParticipantRecord
): ParticipantDTO {
    return ParticipantDTO().apply {
        participantUuid = participantVolumesRec.get(PARTICIPANT_VOLUMES.PARTICIPANT_UUID)
    }
}

Upvotes: 1

Utku &#214;zdemir
Utku &#214;zdemir

Reputation: 7745

You can do it like this:

val list = mutableListOf<Array<Int>>()
list.add(arrayOf(1, 1))

Edit: As Animesh Sahu (thanks!) has pointed out in the comments, if you don't need boxed integers (no nulls in the arrays), you can use the primitive arrays instead and avoid their overhead:

val list = mutableListOf<IntArray>()
list.add(intArrayOf(1, 1))

Upvotes: 1

Related Questions