将对象附加到Scala中的ListBuffer的问题

Zwa*_*ann 4 collections scala append listbuffer

我有问题将对象附加到scala.collection.mutable.ListBuffer.我熟悉相应的API,并且知道通常使用+ =或++ =方法添加对象或对象序列.

我正在实施一个网络支持的纸牌游戏,并且有一个简单的问题,即将一些选择的卡添加到手牌列表中.在下面的代码中,我将获得对手册列表(ListBuffer)的引用,打印ListBuffer的大小,将选定的卡添加到其中并再次打印大小.

// get the references and ensure that it are rally ListBuffers / Lists
val handCards: mutable.ListBuffer[ClientCard] = playerPanel.player.handCards
val chosenCards: List[ClientCard] = _chosenCards

// print the number of elements per list
println("number of hand cards: " + handCards.size)
println("number of chosen cards: " + chosenCards.size)

// append the chosen cards to the hand cards
println("append operation: " + handCards + " ++= " + chosenCards)
handCards ++= chosenCards

// print the number of hand cards again
println("number of hand cards: " + handCards.size)
Run Code Online (Sandbox Code Playgroud)

结果可以预期,手牌的大小会增加所选卡的大小.但是输出是(格式化的):

number of hand cards: 5
number of chosen cards: 2

append operation: ListBuffer(
    rftg.card.Card$$anon$1@1304043, 
    rftg.card.Card$$anon$1@cb07ef, 
    rftg.card.Card$$anon$1@176086d, 
    rftg.card.Card$$anon$1@234265, 
    rftg.card.Card$$anon$1@dc1f04
) ++= List(
    rftg.card.Card$$anon$1@1784427, 
    rftg.card.Card$$anon$1@c272bc
)

number of hand cards: 5
Run Code Online (Sandbox Code Playgroud)

所以元素没有附加.

ClientCard始终是"真实卡"的代表,仅包含绘制卡所需的信息.

trait ClientCard extends AnyRef with ClientObject with CardLike

trait ClientObject extends Serializable {
    def uid: Int
}

trait CardLike {
    val imagePath: String
}
Run Code Online (Sandbox Code Playgroud)

ClientCard在Card类中创建:

def clientCard = new ClientCard() {
    val uid = Card.this.hashCode()
    val imagePath = CardTemplate.cardFolder + Card.this.imageFilename
}
Run Code Online (Sandbox Code Playgroud)

还有ClientPlayer("真实玩家"的代表),其中创建了ListBuffer:

// definition of ClientPlayer trait
trait ClientPlayer extends ClientObject {
    val victoryPoints: Int
    val handCards: mutable.ListBuffer[ClientCard] 
    val playedCards: mutable.ListBuffer[ClientCard]
}

// piece of code to create a client player
def clientPlayer = new ClientPlayer() {
    val uid = Player.this.hashCode()
    val victoryPoints = Player.this.victoryPoints

    val handCards = new mutable.ListBuffer[ClientCard]
    handCards ++= (Player.this.handCards.map(_.clientCard)) 

    val playedCards = new mutable.ListBuffer[ClientCard]
    playedCards ++= Player.this.playedCards.map(_.clientCard)
}
Run Code Online (Sandbox Code Playgroud)

有谁知道这里出了什么问题?或者更通用:有什么情况可以阻止成功将对象附加到ListBuffer?

编辑:有些东西我忘了提及什么似乎导致这种奇怪的行为.创建handCards ListBuffer后,它将通过网络发送,因此将被再次序列化和反序列化.

在Rex Kerr的评论之后,我尝试为ClientPlayer创建一个深层复制方法,并在收到后立即复制每个ClientPlayer.这解决了这个问题.有没有人对这种行为有解释?

Rex*_*err 5

反序列化产生了极其脆弱ListBuffer.这可能是一个错误,但作为一种解决方法,你唯一应该做的就是立即将它添加到其他集合中(例如通过toList它,或者将它添加到空ListBuffer).

这里有一些代码可用于验证序列化/反序列化是否有问题:

import collection.mutable.ListBuffer
import java.io._
val baos = new ByteArrayOutputStream
val oos = new ObjectOutputStream(baos)
oos.writeObject( ListBuffer(1,2,3) )
val bais = new ByteArrayInputStream( baos.toByteArray )
val ois = new ObjectInputStream(bais)
val lb = ois.readObject.asInstanceOf[ListBuffer[Int]]
val lb2 = ListBuffer[Int]() ++= lb
lb2 ++= List(1)  // All okay
lb ++= List(1)  // Throws an exception for me
Run Code Online (Sandbox Code Playgroud)

我将提交错误报告,但暂时应该依靠ListBuffer反序列化时,在一个合理的状态,而是重建.(您可能希望序列化和反序列化List.)