- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why am I getting 0-length ArrayBuffers?
Fri, 2011-05-13, 02:12
scala> import scala.collection.mutable.ArrayBufferimport scala.collection.mutable.ArrayBuffer
scala> new ArrayBuffer[Int](3)res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> res1res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> res1.lengthres3: Int = 0
This does not work at all like I would have thought reading the documentation.
scala> new ArrayBuffer[Int](3)res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> res1res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> res1.lengthres3: Int = 0
This does not work at all like I would have thought reading the documentation.
However, the companion object apply method takes anything you pass to it and makes an ArrayBuffer containing that.
So:
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> ArrayBuffer(3) // This is the apply method on the companion
res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(3)
scala> new ArrayBuffer[Int](3) { def seeInside = array }
res1: scala.collection.mutable.ArrayBuffer[Int]{def seeInside: Array[AnyRef]} = ArrayBuffer()
scala> res1.seeInside
res2: Array[AnyRef] = Array(null, null, null)
--Rex
On Thu, May 12, 2011 at 9:12 PM, Ken McDonald <ykkenmcd@gmail.com> wrote: