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?

4
  • Check out this post stackoverflow.com/questions/2386781/… or this stackoverflow.com/questions/707354/… Commented Jul 4, 2012 at 8: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. Commented Jul 4, 2012 at 8: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. Commented Jul 4, 2012 at 9: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... Commented Jul 4, 2012 at 23:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.