Kotlin .add 覆盖 MutableList 中列表的所有项目

Kotlin .add overriding all items of list in MutableList

我有一个包含可变列表的对象

object TrackingEventsList {
    var EventDate: String = ""
    var EventDescription: String = ""
}

object Waybill {
    var WaybillNumber: String = ""
    var OriginHub: String = ""
    var TrackingEvents: MutableList<TrackingEventsList> = ArrayList()
}

当我尝试添加时 Waybill.TrackingEvents 所有以前的实例都被覆盖并复制最后添加的 TrackingEvent。

private fun fillTracking(events: NodeList) {
    var list = TrackingEventsList
    for (x: Int in 0 until events.length) {
        var tName = (events.item(x) as Element).tagName
        var event = (events.item(x).firstChild as Text).wholeText
        if (tName == "EventDate") {
            list.EventDate = event
        }
        if (tName == "EventDescription") {
            list.EventDescription = event
        }
    }
    Waybill.TrackingEvents.plus(list)
}

调用fillTracking 3次后的结果:

Waybill.TrackingEvents[0].EventDescription = "Event3"
Waybill.TrackingEvents[1].EventDescription = "Event3"
Waybill.TrackingEvents[2].EventDescription = "Event3"

object在Kotlin中是一个单例,也就是说你不能初始化它,它全局只有一个实例。所以当你更改单个实例的项目时,你会覆盖以前的数据。

您应该将它们(或至少 TrackingEventsList)都更改为 class。如果 Waybill 的变量是实例敏感的,它也需要是 class。但是在您添加的代码中,我找不到任何可以说您将其用作单例的内容,因此我将其保留为一个。

class TrackingEventsList (
        var eventDate: String = "",
        var eventDescription: String = "")

/**
 * Also want to point out that this is still a singleton. If the data inside is instance-specific, you need to change it 
 * to a class. 
 */
object Waybill {
    var waybillNumber: String = ""
    var originHum: String = ""
    var trackingEvents: MutableList<TrackingEventsList> = ArrayList()
}

private fun fillTracking(events: NodeList) {
    val item = TrackingEventsList()
    for (x: Int in 0 until events.length) {
        var tName = (events.item(x) as Element).tagName
        var event = (events.item(x).firstChild as Text).wholeText

        if (tName == "EventDate") {
            item.eventDate = event
        }
        if (tName == "EventDescription") {
            item.eventDescription = event
        }
    }

    Waybill.trackingEvents.add(item)
}

并且您应该查看 Kotlin 的命名约定;字段永远不会以大写字母开头,除非它是一个静态常量(在这种情况下它是全大写的)

因为 TrackingEventsList 是一个对象,这意味着它是一个单例(或只有一个实例)。当你通过你的循环时,你总是更新你的 [=12= 的同一个实例] 对象。

TrackingEventsList改成这样:

data class TrackingEventsList(var eventDate: String, var eventDescription: String)

每次执行循环时创建一个新实例,然后在最后将其添加到列表中:

private fun fillTracking(events: NodeList) {
    var eventDate: String = ""
    var eventDescription: String = ""
    for (x: Int in 0 until events.length) {
        val tName = (events.item(x) as Element).tagName
        val event = (events.item(x).firstChild as Text).wholeText
        if (tName == "EventDate") {
            eventDate = event
        }
        if (tName == "EventDescription") {
            eventDescription = event
        }
    }
    Waybill.TrackingEvents.plus(TrackingEventsList(eventDate, eventDescription))
}