加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 10:03:09 所属栏目:安全 来源:网络整理
导读:我有问题将对象附加到 scala.collection.mutable.ListBuffer.我熟悉相应的API,并且知道通常使用=或=方法添加对象或对象序列. 我正在实施一个网络支持的纸牌游戏,并且有一个简单的问题,即将一些选择的卡添加到手牌列表中.在下面的代码中,我将获得对手册列表(L
我有问题将对象附加到 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)

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

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

所以元素没有附加.

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

trait ClientCard extends AnyRef with ClientObject with CardLike

trait ClientObject extends Serializable {
    def uid: Int
}

trait CardLike {
    val imagePath: String
}

ClientCard在Card类中创建:

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

还有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)
}

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

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

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

解决方法

反序列化会产生一个非常脆弱的ListBuffer.这可能是一个错误,但作为一种解决方法,你唯一应该做的就是立即将它添加到其他一些集合中(例如通过toListing它,或者将它添加到一个空的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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读