首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >List<List<Char>> + List<Char> = List<Any>?

List<List<Char>> + List<Char> = List<Any>?
EN

Stack Overflow用户
提问于 2018-03-30 16:11:16
回答 2查看 101关注 0票数 1

我有一个下面的代码是有效的。

代码语言:javascript
复制
class ListManipulate(val list: List<Char>, val blockCount: Int) {

    val result: MutableList<List<Char>> = mutableListOf()

    fun permute(sequence: List<Int> = emptyList(), start: Int = 0, count: Int = blockCount) {
        if (count == 0) {
            result.add(constructSequence(sequence))
            return
        }
        for (i in start .. list.size - count) {
            permute(sequence + i, i + 1, count - 1)
        }
    }

    private fun constructSequence(sequence: List<Int>): List<Char> {
        var result = emptyList<Char>()
        for (i in sequence) {
            result += list[i]
        }
        return result
    }
}

但是,当我将result从MutableList更改为normal List时,即

代码语言:javascript
复制
    var result: List<List<Char>> = emptyList()
    // ...
            result += constructSequence(sequence)

我得到了这个错误Type mismatch. Require: List<List<Char>>; Found: List<Any>

完整代码如下

代码语言:javascript
复制
class ListManipulate(val list: List<Char>, val blockCount: Int) {

    var result: List<List<Char>> = emptyList()

    fun permute(sequence: List<Int> = emptyList(), start: Int = 0, count: Int = blockCount) {
        if (count == 0) {
            result += constructSequence(sequence)
            return
        }
        for (i in start .. list.size - count) {
            permute(sequence + i, i + 1, count - 1)
        }
    }

    private fun constructSequence(sequence: List<Int>): List<Char> {
        var result = emptyList<Char>()
        for (i in sequence) {
            result += list[i]
        }
        return result
    }
}

为什么result + constructSequence(sequence)会生成List<Any>而不是List<List<Char>>

有没有办法我仍然可以使用普通的List>而不是可变列表?

EN

回答 2

Stack Overflow用户

发布于 2018-03-30 16:16:18

按住Ctrl键并单击IDEA中的+,您将看到它会将您带到以下功能:

代码语言:javascript
复制
/**
 * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection.
 */
public operator fun <T> Collection<T>.plus(elements: Iterable<T>): List<T> {
    /* ... */
}

这意味着您要将elements的所有单独元素添加到接收器。也就是说,您将把所有的T添加到List<List<T>>中。由于List<T>不是T,因此您将得到List<Any>

票数 2
EN

Stack Overflow用户

发布于 2018-03-30 20:30:34

问题是+=overloaded。如果它看到IterableArraySequence,它的行为会有所不同。你必须显式地使用plusElement()来实现你想要的行为。

考虑以下代码:

代码语言:javascript
复制
class ListManipulate(val list: List<Char>, val blockCount: Int) {

    var result: List<List<Char>> = emptyList()

    fun permute(sequence: List<Int> = emptyList(), start: Int = 0, count: Int = blockCount) {
        if (count == 0) {
            result = result.plusElement(constructSequence(sequence))
            return
        }
        for (i in start..list.size - count) {
            permute(sequence + i, i + 1, count - 1)
        }
    }

    private fun constructSequence(sequence: List<Int>): List<Char> =
        List(sequence.size, { i -> list[sequence[i]] })
}

附言:我还冒失地将你的constructSequence()更新为更简洁的东西。

顺便说一句:+=在内部使用addAll

/** *返回一个列表,其中包含原始集合的所有元素,然后返回给定elements集合的所有元素。*/ public运算符FUN Collection.plus (elements : Iterable):List { if (Elements is Collection) { val result = ArrayList(this.size + elements.size) result.addAll(this) result.addAll(elements) return result } else { val result = ArrayList(this) result.addAll(elements) return result }}

附注:您还可以执行以下操作:

代码语言:javascript
复制
 result.toMutableList().add(constructSequence(sequence))

返回一个MutableList是很好的,唯一的区别是List接口没有操作方法。在内部,两者都由一个ArrayList表示

@SinceKotlin("1.1") @kotlin.internal.InlineOnly公共内联fun列表(大小: Int,init:(index: Int) -> T):List = MutableList(size,init)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49570809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档