I understand the basic concepts behind both, but I am wondering which is best to use for what type of structure for optimal complexity.
Is the general consensus that you simply use Array
when you know the length of the structure you're creating and then ArrayBuffer
when the length is unknown?
However, what confuses me is that it is stated Array
is more efficient for built-in Scala types such as Int
, String
and so on. Does this also apply to the use case where you simply initiate an Array[Int]
and then add values to it with :+=
or is it only when it has the fixed length?
I assume that in cases where you know the length is unknown and you are working with datatypes other than the built-in ones the best solution would be ArrayBuffer
, but I'm unsure of when built-in ones are used.
And what if you have a matrix, lets say Array[Array[Int]]
. Is this the most optimal if you will be adding rows to it with :+=
? Or maybe in that case it should be ArrayBuffer[Array[Int]]
?