作者:痴情被撕碎的阳光 | 来源:互联网 | 2022-11-20 19:42
1> Ahmed Hegazy..:
代码中的错误是您在循环的每次迭代中都列出了一个列表。您应该先创建列表,然后将循环中的每个项目添加到列表中!
fun convertRoomClass(courses: List) : List {
val newList = mutableListOf()
courses.forEach {
val id = it.pathID
val name = it.pathName
val desc = it.pathDescription
newList += AnotherCourseClass(id, name, desc)
}
return newList
}
更好的解决方案是使用地图功能
fun convertRoomClass(courses: List) = courses.map {
AnotherCourseClass(it.pathID, it.pathDescription, it.pathDescription)
}