Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to create a dynamically growing array in Scala?

Tags:

scala

I wanted to add items dynamically into an array. But it seems Scala arrays and lists don't provide any methods for adding items dynamically due to the immutable nature.

So I decided to use List data type to make use of this :: method to achieve this. My code look like this

var outList = List(Nil) val strArray = Array("ram","sam","bam")  for (str<-strArray)      outList = str :: outList 

Though it works in some way, the problem is the new strings are pre-appended into the list. But the ideal requirement is order of the data. Yeah, I know what you are thinking, you can reverse the final result list to get the original order. But the problem is it's a huge array. And I believe it's not a solution though it solves the problem. I believe there should be a simple way to solve this...

And my reason for hacking Scala is to learn the functional way of coding. Having var (mutable type) and populating the list on the fly seems to me is not a functional way of solving things.

How can I do it?

Ideally, I want to achieve something like this in Scala (below the C# code)

List<int> ls = new List<int>();     for (int i = 0; i < 100; i++)     ls.Add(i); 
like image 817
RameshVel Avatar asked Jul 30 '10 05:07

RameshVel


People also ask

How do you dynamically increase the size of an array?

Allocate a new[] array and store it in a temporary pointer. Copy over the previous values that you want to keep. Delete[] the old array. Change the member variables, ptr and size to point to the new array and hold the new size.

Can array grow dynamically?

One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements your array will hold ahead of time. A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.

What is a dynamic array How is it created give a typical example of use of a dynamic array?

In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern mainstream programming languages.


1 Answers

But it seems Scala Arrays & Lists doesn't provide any methods for adding items dynamically due to the immutable nature.

Well, no. Scala Arrays are just Java arrays, so they are mutable:

val arr = Array(1,2)  arr(0) = 3 // arr == Array(3, 2) 

But just as Java (and C/C++/C#/etc.) arrays, you can't change the size of an array.

So you need another collection, which is backed by an array, but does allow resizing. A suitable collection in Scala is scala.collection.mutable.ArrayBuffer, java.util.ArrayList in Java, etc.

If you want to get a List instead of an Array in the end, use scala.collection.mutable.ListBuffer instead.

like image 121
Alexey Romanov Avatar answered Sep 22 '22 21:09

Alexey Romanov