Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.33 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Problems with appending objects to a ListBuffer in Scala
  2. // get the references and ensure that it are rally ListBuffers / Lists
  3. val handCards: mutable.ListBuffer[ClientCard] = playerPanel.player.handCards
  4. val chosenCards: List[ClientCard] = _chosenCards
  5.  
  6. // print the number of elements per list
  7. println("number of hand cards: " + handCards.size)
  8. println("number of chosen cards: " + chosenCards.size)
  9.  
  10. // append the chosen cards to the hand cards
  11. println("append operation: " + handCards + " ++= " + chosenCards)
  12. handCards ++= chosenCards
  13.  
  14. // print the number of hand cards again
  15. println("number of hand cards: " + handCards.size)
  16.        
  17. number of hand cards: 5
  18. number of chosen cards: 2
  19.  
  20. append operation: ListBuffer(
  21.     rftg.card.Card$$anon$1@1304043,
  22.     rftg.card.Card$$anon$1@cb07ef,
  23.     rftg.card.Card$$anon$1@176086d,
  24.     rftg.card.Card$$anon$1@234265,
  25.     rftg.card.Card$$anon$1@dc1f04
  26. ) ++= List(
  27.     rftg.card.Card$$anon$1@1784427,
  28.     rftg.card.Card$$anon$1@c272bc
  29. )
  30.  
  31. number of hand cards: 5
  32.        
  33. trait ClientCard extends AnyRef with ClientObject with CardLike
  34.  
  35. trait ClientObject extends Serializable {
  36.     def uid: Int
  37. }
  38.  
  39. trait CardLike {
  40.     val imagePath: String
  41. }
  42.        
  43. def clientCard = new ClientCard() {
  44.     val uid = Card.this.hashCode()
  45.     val imagePath = CardTemplate.cardFolder + Card.this.imageFilename
  46. }
  47.        
  48. // definition of ClientPlayer trait
  49. trait ClientPlayer extends ClientObject {
  50.     val victoryPoints: Int
  51.     val handCards: mutable.ListBuffer[ClientCard]
  52.     val playedCards: mutable.ListBuffer[ClientCard]
  53. }
  54.  
  55. // piece of code to create a client player
  56. def clientPlayer = new ClientPlayer() {
  57.     val uid = Player.this.hashCode()
  58.     val victoryPoints = Player.this.victoryPoints
  59.  
  60.     val handCards = new mutable.ListBuffer[ClientCard]
  61.     handCards ++= (Player.this.handCards.map(_.clientCard))
  62.  
  63.     val playedCards = new mutable.ListBuffer[ClientCard]
  64.     playedCards ++= Player.this.playedCards.map(_.clientCard)
  65. }
  66.        
  67. import collection.mutable.ListBuffer
  68. import java.io._
  69. val baos = new ByteArrayOutputStream
  70. val oos = new ObjectOutputStream(baos)
  71. oos.writeObject( ListBuffer(1,2,3) )
  72. val bais = new ByteArrayInputStream( baos.toByteArray )
  73. val ois = new ObjectInputStream(bais)
  74. val lb = ois.readObject.asInstanceOf[ListBuffer[Int]]
  75. val lb2 = ListBuffer[Int]() ++= lb
  76. lb2 ++= List(1)  // All okay
  77. lb ++= List(1)  // Throws an exception for me