English 中文(简体)
在Schala的S ListBuffer身上的 app物问题
原标题:Problems with appending objects to a ListBuffer in Scala

我有问题,把物体射入可变的 s。 ListBuffer. 我很熟悉相关的APIC,并知道你通常使用+=或++=方法添加物体或 Objects。

我在网络支持下开展了一次卡片游戏,并有一个简单的问题,即在手卡清单中添加一些。 在以下法典中,我将参考手卡清单,印刷名单Buffer的体积,在上面添加chosen卡,并重印尺寸。

// 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

因此,这些内容没有附后。

客户 贺卡一向是“真实卡”的代表,只包括提取卡所需的信息。

trait ClientCard extends AnyRef with ClientObject with CardLike

trait ClientObject extends Serializable {
    def uid: Int
}

trait CardLike {
    val imagePath: String
}

客户 贺卡在卡级:

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

还有一个客户(一个“真实角色”的代表)创建名单B公司:

// 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)
}

谁知道这里有什么错误? 或者说更笼统:有哪些情况可以防止将物体成功射入名单Buffer?

Edit: There is something that I forgot to mention and what seemed to cause this strange behaviour. After creation of the handCards ListBuffer it is being sent over network and is therefore being serialized and deserialized again.

在Rex Kerr发表评论后,我试图为客户Player制定深入细微的方法,并在对用户Player进行检索后复制。 这个问题已经解决。 是否有人对此行为作出解释?

最佳回答

蒸汽产生异常脆弱的<密码>。 这可能是一种泡沫,但作为一种工作,只有。 你应当做的是,在其他一些收集中(例如,通过<编码>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

我提交了简要报告,但现在你应当而不是<>em>依靠<代码>ListBuffer在空气化后处于敏感状态,而是加以重建。 (你不妨将<条码>List改为>。)

问题回答

暂无回答




相关问题
Is HashMap in Java collision safe

I am developing a parser that needs to put key value pairs in hashmap. A key can have multiple values which I can do in this way HashMap<String,ArrayList<String>> . What happens if the ...

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

PLSQL Collections - how to use table of records?

I m new to PL/SQL and I m trying to use a table of records, but I don t know how to use this feature. What is the problem? DECLARE TYPE TIP IS RECORD ( F1 ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...