1

In my application I want to have a dictionnary where the key is an integer.

Since it's an integer, I use normal Array :

var arr : Array = [];
arr[5] =  anObject;
arr[82] =  anOtherObject;

When I iterate with for each, no problem, it iterates through those 2 object. The problem is that arr.length return 83... So I have to create a variable that count the number as I modify the array.

Question 1 : Is there a best practice for that (IE: associative array with int as key)? I hesitated to use a Dictionnary.

Question 2 : Does flash allocates memory for the unused buckets of the array?

tibo
  • 5,326
  • 4
  • 37
  • 53
  • Check out this post http://stackoverflow.com/questions/2386781/get-size-of-actionscript-3-dictionary or this http://stackoverflow.com/questions/707354/calculating-dictionary-length-in-flex – Dennis Jaamann Jul 04 '12 at 08:24
  • Sorry you are out of the scope, I know how to calculate the size of an associative array... That's not my question... My question is how to deal with associative with an int as key since we can use them with Array. – tibo Jul 04 '12 at 08:37
  • Exactly and those questions hold your answer. Basically Array, Object or Dictionary can be used to achieve the same thing. An associative sparse Array. – Dennis Jaamann Jul 04 '12 at 09:27
  • If I said "I hesitated to use a Dictionnary", it means that I was aware of this solution but I can't figured out what the best solution. The object solution is definetly not relevant for my problem. That's why I asked for a best practice, not for links on how to use Dictionnary... – tibo Jul 04 '12 at 23:54

1 Answers1

3

Arrays in flash are sparse (unlike Vector), so the empty entries will not be allocated. If you need to know the length, you will probably need to keep track of it manually (make a wrapper class perhaps).

Adobe says:

Arrays are sparse arrays, meaning there might be an element at index 0 and another at index 5, but nothing in the index positions between those two elements. In such a case, the elements in positions 1 through 4 are undefined, which indicates the absence of an element, not necessarily the presence of an element with the value undefined.

Jonatan Hedborg
  • 4,382
  • 21
  • 30