tibo
tibo

Reputation: 5474

Associative array with int as key

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?

Upvotes: 1

Views: 769

Answers (1)

Jonatan Hedborg
Jonatan Hedborg

Reputation: 4432

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.

Upvotes: 3

Related Questions