- Problems with appending objects to a ListBuffer in Scala
- // 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
- }
- // 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)
- }
- 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