【问题标题】:How to add MutableList to an other MutableList - Kotlin?如何将 MutableList 添加到另一个 MutableList - Kotlin?
【发布时间】:2020-11-14 07:23:25
【问题描述】:

下午好,亲爱的 StackOverflow 社区,

我在 Kotlin 中使用 MutableList 时遇到问题。更具体地说,我没有成功在 MutableList 中添加 MutableList

比如后面的例子

fun main() {

    var mutableListIndex: MutableList<Int> = mutableListOf<Int>()
    var mutableListTotal: MutableList<MutableList<Int>> = mutableListOf<MutableList<Int>>()

    for(i in 0..5) {
        mutableListIndex.add(i)
        println(mutableListIndex)

        mutableListTotal.add(mutableListIndex)
        println(mutableListTotal)

    }
}

我得到以下结果

[0]
[[0]]
[0, 1]
[[0, 1], [0, 1]]
[0, 1, 2]
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
[0, 1, 2, 3]
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
[0, 1, 2, 3, 4]
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
[0, 1, 2, 3, 4, 5]
[[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]

虽然,我期待之后的结果

[0]
[[0]]
[0, 1]
[[0], [0, 1]]
[0, 1, 2]
[[0], [0, 1], [0, 1, 2]]
[0, 1, 2, 3]
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]
[0, 1, 2, 3, 4]
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
[0, 1, 2, 3, 4, 5]
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5]]

我无法理解我错在哪里,因为在我看来,从严格的算法角度来看,代码是好的。

有人可以帮我解释一下我的错误吗?

真诚的

【问题讨论】:

  • mutableListIndex 正在发生变异,它是堆中的单个对象,对它的更改会反映在 mutableListTotal 列表中。
  • 亲爱的 Animesh Sahu,我明白你的意思。您知道防止这种现象发生的解决方案吗?
  • 创建一个新列表并添加它,mutableListTotal.add(mutableListIndex.toList()) toList() 或 toMutableList() 浅拷贝列表的内容并使用它们创建一个新列表。

标签: kotlin mutablelist


【解决方案1】:

按照上述 Animesh Sahu 爵士的建议,我终于遵循了这个解决方案:

fun main() {

    var mutableListIndex: MutableList<Int> = mutableListOf<Int>()
    var mutableListTotal: MutableList<MutableList<Int>> = mutableListOf<MutableList<Int>>()

    for(i in 0..5) {


        mutableListIndex.add(i)
        println(mutableListIndex)


        mutableListTotal.add(mutableListIndex.toMutableList())
        println(mutableListTotal)

    }
}

哪个给:

[0]
[[0]]
[0, 1]
[[0], [0, 1]]
[0, 1, 2]
[[0], [0, 1], [0, 1, 2]]
[0, 1, 2, 3]
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]
[0, 1, 2, 3, 4]
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
[0, 1, 2, 3, 4, 5]
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5]]

非常感谢大家的及时回复和帮助

真诚的

【讨论】:

    【解决方案2】:

    您始终传递mutableListIndex 的相同引用以添加到mutableListTotal。所以在每个位置上你都有相同的对象。

    然后您将一个新项目添加到您的第一个列表中,并且对它的每个引用都指向更新后的列表,还有一个项目。

    要获得一个独立的对象,它不会在您的第一个引用每次更新时都更新,您首先需要创建 List 的副本,并且只将副本添加到您的第二个列表中。这样一来,对初始列表的更新将不会反映到您的第一个列表副本中。

    import java.util.List.copyOf
    
    fun main() {
    
        ...
            mutableListTotal.add(copyOf(mutableListIndex))
        ...
    }
    

    【讨论】:

    • 亲爱的 Simulant,我终于在我的第一条消息的评论中遵循了 Animesh Sahu 爵士的回答最后,代码如下所示: `` fun main() { var mutableListIndex: MutableList = mutableListOf() var mutableListTotal: MutableList> = mutableListOf>() for(i in 0..5) { mutableListIndex.add(i) println(mutableListIndex) mutableListTotal.add( mutableListIndex.toMutableList()) println(mutableListTotal) } } ``` 好像可行
    猜你喜欢
    • 2016-01-21
    • 2019-05-21
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多