0

I'm trying to append ArrayBuffer(s) in an ArrayBuffer of ArrayBuffer but the result seems weird to me.

 def main(args: Array[String]) {
    println(initArray(List(1,2,5,20)))
 }

 def initArray(numbers: List[Int]): Int = {
     var combination = ArrayBuffer[Int]()
     var combinations = ArrayBuffer[ArrayBuffer[Int]]()
     var finishedCombinations = ArrayBuffer[ArrayBuffer[Int]]()
     // Init
     for (number <- numbers) {
       combination.clear()
       combination.append(number)
       println("combination : "+combination)
       combinations.insert(0,combination)
       println("combinations : "+combinations)
     }
     combinations.size
 }

I'm getting an ArrayBuffer of size 4 as expected but the content in unexpected :

Was expecting to get :

combinations : ArrayBuffer(ArrayBuffer(1), ArrayBuffer(2), ArrayBuffer(5), ArrayBuffer(20))

but i get :

combinations : ArrayBuffer(ArrayBuffer(20), ArrayBuffer(20), ArrayBuffer(20), ArrayBuffer(20))

This behaviour is unclear to me, can anyone help ?

Regards.

2 Answers 2

1

Step through your List turning each element into an ArrayBuffer[Int] and then append each to the result buffer.

import collection.mutable.ArrayBuffer

def initArray(numbers: List[Int]): Int = {
  val combinations = ArrayBuffer[ArrayBuffer[Int]]()
  numbers.map(ArrayBuffer() += _).foreach(combinations.append(_))
  combinations.size
}

Of course this is all rather pointless since you're building a buffer, returning its size (which is the same size as the input List), and then discarding the buffer you just built. I'm assuming that this is just a rough outline for something larger and actually useful.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi jwvh, thanks for your reply. As you guessed, this code is a sample of something larger : i'm usually using Scala with Spark for DataScience pipeline, with vectors, Dataset/frames and ml lib. Anyway decided to go deeper into Scala through some moocs and personnal exploration. This code is part of that.
Hi jwvh, thanks for your reply. As you guessed, this code is a sample of something larger : i'm usually using Scala with Spark for DataScience pipeline, with vectors, Dataset/frames and ml lib. Anyway decided to go deeper into Scala This code is part of that try. I've a question regading your answer : you set combinations as a val but you then dynamically allocate information into it; uderstood val was used to set immutable values. Would have expect the val size to be set with declaration and allocation not to be done in a loop. What did i understand wrong about using immutable structures ?
I made combinations a val because it references ("points at"/"contains") a single ArrayBuffer and that never changes, so a val is appropriate. The ArrayBuffer itself is mutable so its contents can change over time. When dealing with mutable data you should choose between a val that references a mutable data structure, or a var that references an immutable data structure. For example: 5 is an immutable Int, it can't become something else, but var x = 5 means that x can change to reference a different immutable Int. You seldom (never) want to reference a mutable with var.
0

You are repeatedly appending the same arraybuffer[int] instance to the main arraybuffer.since an array buffer is mutable when you print it at the end it shows the attributes consistent with all the uses you've made of it. Clear() clears the contents but does not deallocate the memory span hiding behind the reference. You perhaps should create a new arraybuffer afresh in the loop itself, or better, use a fresh immutable vector Instead of an arraybuffer.

1 Comment

Hi remi,Thanks for you reply. You totally right, just moved the var combination = ArrayBuffer[Int]() into the loop and the previous allocation is put away. Considering this, what's the use case of the clear() method ? Regards.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.