更新列表Kotlin中的对象

更新列表Kotlin中的对象,kotlin,Kotlin,我正在尝试修改我的allVehicles变量,因此它应该包含problems列表中的vehiclesWithProblems信息 这是我的代码的简化版本,但我需要更新的所有车辆列表,其中包含更换件,并删除旧的(没有问题的) 我怎样才能做到这一点?此代码不起作用,所有车辆列表保持不变 data class VehicleContainer( val id: Int, val vehicles: List<Vehicle> ) data class Ve

我正在尝试修改我的
allVehicles
变量,因此它应该包含
problems
列表中的
vehiclesWithProblems
信息

这是我的代码的简化版本,但我需要更新的
所有车辆
列表,其中包含
更换件
,并删除旧的(没有
问题的

我怎样才能做到这一点?此代码不起作用,
所有车辆
列表保持不变

data class VehicleContainer(
        val id: Int,
        val vehicles: List<Vehicle>
)

data class Vehicle(
        val id: Int,
        val name: String,
        val problem: Problem
)

data class Problem(
        val quantity: Int,
        val problemList: List<Int>,
        val info: String? = ""
)

fun main() {
    val vehiclesWithProblems = listOf<Vehicle>() //list of vehicles with problems - wont be empty
    val allVehicles = mutableListOf<Vehicle>()//list of all vehicles (initially without any problems, but won't be empty either)

    allVehicles.forEachIndexed { index, vehicle ->
        val newVehicle = vehiclesWithProblems.find { vehicleWithProblem -> vehicle.id == vehicleWithProblem.id }
        if (newVehicle != null) {
            val replacement = vehicle.copy(problem = Problem(
                    quantity = newVehicle.problem.quantity,
                    problemList = newVehicle.problem.problemList,
                    info = newVehicle.problem.info)
            )
            allVehicles[index] = replacement
        }
    }
}
数据类车辆容器(
val id:Int,
val车辆:列表
)
数据级车辆(
val id:Int,
val name:String,
问题:问题
)
数据类问题(
val数量:Int,
val问题列表:列表,
val信息:字符串?=“”
)
主要内容(){
val vehiclesWithProblems=listOf()//有问题的车辆列表-不会为空
val allVehicles=mutableListOf()//所有车辆的列表(最初没有任何问题,但也不会为空)
allVehicles.Forached{索引,车辆->
val newVehicle=vehiclesWithProblems.find{vehicleewithproblems->vehicle.id==vehicleewithproblems.id}
如果(新车!=null){
val更换=车辆。复制(问题=问题(
数量=新车。问题。数量,
problemList=newVehicle.problem.problemList,
信息=新车。问题。信息)
)
所有车辆[索引]=更换
}
}
}

在我看来,allVehicles列表实际上已被修改,但请注意!您制作了一份车辆的副本,其中只有问题被更改,其余的保持不变。运行下面的代码,您将看到循环后,«Tesla without a problem»仍在列表中,但现在出现了问题(因此列表实际上已更改):


当你运行它时会发生什么?什么不起作用?所有车辆列表保持不变。我将编辑问题以澄清它。问题是在我的实际代码中,我使用newVehicle.copy而不是vehicle.copy。但多亏了你,我才意识到。非常感谢你!!
fun main() {
    val vehiclesWithProblems = listOf(Vehicle(1, "Tesla", Problem(1, listOf(1), "Problem #1"))) //list of vehicles with problems - wont be empty
    val allVehicles = mutableListOf(Vehicle(1, "Tesla without a problem", Problem(0, listOf(0), "No problem")))//list of all vehicles (initially without any problems, but won't be empty either)

    println("vehiclesWithProblems: $vehiclesWithProblems")
    println("allVehicles: $allVehicles")

    allVehicles.forEachIndexed { index, vehicle ->
        val newVehicle = vehiclesWithProblems.find { vehicleWithProblem -> vehicle.id == vehicleWithProblem.id }
        if (newVehicle != null) {
            val replacement = vehicle.copy(problem = Problem(
                    quantity = newVehicle.problem.quantity,
                    problemList = newVehicle.problem.problemList,
                    info = newVehicle.problem.info)
            )
            println("Changing #$index!")
            allVehicles[index] = replacement
        }
    }

    println("After the loop, allVehicles: $allVehicles")
}