【发布时间】:2021-09-04 14:03:42
【问题描述】:
我正在尝试从 kotlin 中的函数返回 List>>。我是 kotlin 的新手。
编辑1
这就是我的尝试
val a = mutableListOf(mutableListOf(mutableMapOf<String, String>()))
上述变量的问题是,我无法弄清楚如何将数据插入该变量。我试过这个:
val a = mutableListOf(mutableListOf(mutableMapOf<String, String>()))
val b = mutableListOf(mutableMapOf<String, String>())
val c = mutableMapOf<String, String>()
c.put("c", "n")
b.add(c)
a.add(b)
这是给我的:
[[{}], [{}, {c=n}]]
我想要的是 [[{c=n}]]
谁能告诉我如何在其中插入数据?
我要达到的最终目标是以 List>>
编辑 2
我正在尝试为其编写此 dat 结构的函数:
fun processReport(file: Scanner): MutableList<List<Map<String, String>>> {
val result = mutableListOf<List<Map<String, String>>>()
val columnNames = file.nextLine().split(",")
while (file.hasNext()) {
val record = mutableListOf<Map<String, String>>()
val rowValues = file.nextLine()
.replace(",(?=[^\"]*\"[^\"]*(?:\"[^\"]*\"[^\"]*)*$)".toRegex(), "")
.split(",")
for (i in rowValues.indices) {
record.add(mapOf(columnNames[i] to rowValues[i]))
print(columnNames[i] + " : " + rowValues[i] + " ")
}
result.add(record)
}
return result
}
【问题讨论】:
标签: kotlin arraylist data-structures collections