重复列出无法正常使用的数据类列表

时间:2019-05-14 13:54:00

标签: kotlin

我有一个要迭代的数据类列表。 这是数据类及其创建位置。

data class BgaResponse(val columnName: String, val restricted: Boolean, val predictionValue: Double)

val bgaResponse: List<BgaResponse>? = callBgaEndpoint(bgaRequest)

打印bgaResponse显示如下:

[{columnName=col1, restricted=false, predictionValue=0.9963}, {columnName=not_here, restricted=false, predictionValue=0.995941}]

我首先尝试使用forEach:

bgaResponse?.forEach {
            println(it)
        }

这不打印任何内容。接下来,我尝试通过索引访问每个元素:

for(i in 0 until bgaResponse!!.size) {
            println(bgaResponse[i])
        }

这将按预期打印每个元素:

{columnName=col1, restricted=false, predictionValue=0.9963}
{columnName=not_here, restricted=false, predictionValue=0.995941}

我尝试打印bgaResponse[i].columnName,但是没有再次打印。现在这没有任何意义。

最后我尝试:

for(i in 0 until bgaResponse!!.size) {
            val x: BgaResponse= bgaResponse[i]
        }

这应该可以工作,我的IDE告诉我bgaResponse [i]是BgaResponse。这会引发错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.package.something.placeholder.model.BgaResponse

0 个答案:

没有答案
相关问题