Hi,
I wanted to add items dynamically into an array. But it seems Scala Arrays & Lists doesn'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 its a huge array. And i believe its not a solution though it solves the problem. I believe there should be a simple way to solve this..
And my reason to 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.
please advice me..
Update:
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);
Cheers