0

So I am trying to create an immutable array of size 10 within a mutable array in scala. In this immutable array I want to store key, value pairs. so far I have this mutable array:

val array1 = mutable.ArrayBuffer[Option[IndexedSeq[(A,B)]]]()

Now to create an immutable buffer in array1 would I just do:

array1(0) = immutable.ArrayBuffer[](10)

I am confused on what would go within the brackets of the immutable buffer for the type.

3
  • I don't understand what exactly do you want to do? You are trying to assign a immutable.ArrayBuffer to the first position of array1, is what what you want to do? Commented Nov 10, 2021 at 14:29
  • @LuisMiguelMejíaSuárez Yes that is what I am going for Commented Nov 10, 2021 at 14:35
  • 2
    Well array1 is an mutable.ArrayBuffer[Option[IndexedSeq[(A,B)]]] thus the elements must be Option[IndexedSeq[(A,B)]] not an immutable.ArrayBuffer so what you want to do doesn't make sense. Commented Nov 10, 2021 at 14:37

1 Answer 1

1

There is no immutable.ArrayBuffer.  ArrayBuffer is mutable.

Trait IndexedSeq can correspond to both immutable and mutable collections, so you have options what to use as your inner elements:

type A = Int
type B = Int

val array1 = mutable.ArrayBuffer[Option[IndexedSeq[(A,B)]]]()

// Adds immutable Vector
array1 += Some(IndexedSeq[(A, B)]( (1,2), (3,4) ))

// Adds mutable ArrayBuffer
array1 += Some(ArrayBuffer[(A, B)]( (1,2), (3,4) ))

Note, since your inner element is Option[IndexedSeq[...]], you need to wrap the elements you are adding in Some.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.