因此,我正在Kotlin中上课学习基于文本的游戏。我需要知道除了下面的代码还有其他方法。例如,我希望它执行类似的操作
val game:MutableList<MutableList<Char>> = mutableListOf(mutableListOf(' '*6)*7)
游戏:
private val game:MutableList<MutableList<Char>> = mutableListOf(
mutableListOf(' ', ' ', ' ', ' ', ' ', ' '),
mutableListOf(' ', ' ', ' ', ' ', ' ', ' '),
mutableListOf(' ', ' ', ' ', ' ', ' ', ' '),
mutableListOf(' ', ' ', ' ', ' ', ' ', ' '),
mutableListOf(' ', ' ', ' ', ' ', ' ', ' '),
mutableListOf(' ', ' ', ' ', ' ', ' ', ' '),
mutableListOf(' ', ' ', ' ', ' ', ' ', ' ')
)
最佳答案
MutableList(7) { MutableList(6) { ' ' } }
使用
inline fun <T> MutableList(size: Int, init: (index: Int) -> T): MutableList<T>
。其他集合具有类似的工厂功能。
关于list - 如何简化可变列表中可变列表的分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51011718/